Solution to LeetCode's 136. Single Number with JavaScript.
Solution
/**
* @param {number[]} nums
* @return {number}
*/
const singleNumber = (nums) => {
let result = nums[0];
for (let i = 1; i < nums.length; i++) {
result = result ^ nums[i];
}
return result;
};
- Time complexity: O(n)
- Space complexity: O(1)
Using the XOR operator is simple way to implement.
Top comments (0)