Find the Duplicate Number
easySave
ArrayBit ManipulationHash Table
Given an array of integers where each number appears exactly twice except for one number which appears once, find the number that appears only once. The solution should have a linear time complexity and use no extra space.
Example 1
Input
[ 1, 2, 2, 3, 1 ]
Output
3
Explanation. Here, the number '3' is the only number that appears once.
Example 2
Input
[ 4, 7, 4, 5, 5 ]
Output
7
Explanation. In this case, '7' appears only once while all other numbers appear twice.
Example 3
Input
[ 10, 10, 22 ]
Output
22
Explanation. Number '22' is the unique number appearing once.
Follow-up: Can this problem be extended to finding unique elements in an array where every element appears `n` times except for one element?
Constraints:
1. The length of the array is always odd.\n2. All integers in the array except one appear exactly twice.
- Views
- 2