Please let me know is this a best approach to find unique number from an array?
var singleNumber = function(nums) {
for (var i = 0; i < nums.length; i++) {
if (i === 0) {
nums[0] = 0 ^ nums[0];
/* console.log(nums[0]); */
} else {
nums[0] ^= nums[i];
/* console.log(nums[0]); */
}
}
console.log(nums[0]);
return nums[0];
};
singleNumber([2, 2, 3, 3, 4]);
If you have any better approach with this, help me out.