DEV Community

Masaki Fukunishi
Masaki Fukunishi

Posted on

LeetCode #136 Single Number with JavaScript

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;
};
Enter fullscreen mode Exit fullscreen mode
  • Time complexity: O(n)
  • Space complexity: O(1)

Using the XOR operator is simple way to implement.

Top comments (0)