In a given array of numbers, one element will show up once and the rest will show up twice. Can you find that number in O(n) linear time?
eg - lonelyNumber([4, 4, 6, 1, 3, 1, 3]) // 6
I am trying this in javascript and my code so far -
var lonelyNumber=function(arr) {
for(var i=0;i<arr.length;i++) {
for(var j=0;j<arr.length;j++) {
if(j!==i && arr[i]===arr[j]) {
break;
}
}
if(j===arr.length) {
return arr[i];
}
}
}
console.log(lonelyNumber([4, 4, 6, 1, 3, 1, 3]));
But this is having a complexity of O(n^2). How do I get it to O(n)?