Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.50 KB | None | 0 0
  1. /*
  2. * Finds element without a pair in an unsorted array of integers.
  3. * @param {array} A
  4. * @param {int} Element without a pair
  5. */
  6.  
  7. // Brute-force solution (55% on codility)
  8. function solution(A) {
  9. for (let digit of A) {
  10. if(A.indexOf(digit) === A.lastIndexOf(digit)) {
  11. return digit;
  12. }
  13. }
  14. };
  15.  
  16. // Elegant but hard to understand solution using Bitwise XOR operator (100% on codility)
  17. function solution(A) {
  18. let result = 0;
  19.  
  20. for (let element of N) {
  21. result ^= element;
  22. }
  23.  
  24. return result;
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement