Find the Unique Number
easySave
ArrayBit ManipulationHash Table
Given an array of integers where every element appears twice except for one element which appears only once, find and return the unique element.
Example
- Input:
[4, 1, 2, 1, 2] - Output:
4
Example 1
Input
[ 2, 3, 2, 4, 4 ]
Output
3
Explanation. In the array, the number 3 is the only element that appears once.
Example 2
Input
[ 7, 7, 8 ]
Output
8
Explanation. 8 appears only once, while 7 appears twice.
Example 3
Input
[ 1, 1, 2, 2, 3, 3, 4, 6, 6 ]
Output
4
Explanation. All numbers except for 4 appear twice.
Example 4
Input
[ 10, 20, 20 ]
Output
10
Explanation. 10 appears only once, while 20 appears twice.
Follow-up: How would you modify your solution if there are multiple unique numbers and you have to return them in an array?
Constraints:
The array will always contain at least three elements, ensuring there is one unique number.
- Views
- 3