Facebook PixelSingle Number — Coding Practice
Single NumberEasy

Single Number

Easy 13.8k71% acceptance
ArrayBit Manipulation

Given a non-empty array nums in which every element appears exactly twice except for one element that appears once, return that single element.

Aim for linear time and constant extra space.

Example 1
Input: nums = [2,2,1]
Output: 1
Example 2
Input: nums = [4,1,2,1,2]
Output: 4
1 and 2 each appear twice; 4 is the loner.
Example 3
Input: nums = [7]
Output: 7
Constraints
  • 1 ≤ nums.length ≤ 3·10⁴
  • -3·10⁴ ≤ nums[i] ≤ 3·10⁴
  • Every element appears twice except one.
Asked atAmazonGoogleAppleAdobe
JavaScript
Loading editor…
Case 1
[2,2,1]
expected: 1
Case 2
[4,1,2,1,2]
expected: 4
Case 3
[7]
expected: 7
Case 4
[-1,-1,-2]
expected: -2
Case 5
[0,1,0]
expected: 1
Case 6
[5,3,5,4,3]
expected: 4