Find the Two Numbers Appearing Odd Number of Times
Implement findOddOccurringPair
Two values in this array break a pattern the rest all obey: every other number turns up an even number of times — twice, four times, however many — while these two show up an odd number of times each. Track down that pair and hand them back smallest first.
A running XOR across the whole array cancels every even-count value down to nothing no matter how many times it repeats, leaving only the XOR of the two odd ones combined. Pulling them apart just needs one bit where they're guaranteed to disagree — using that bit to split the whole array into two independent groups leaves exactly one of the pair standing in each group after XOR-ing within it.
Example 1:
Input: nums = [4,2,4,5,2,3,3,1]
Output: [1,5]
Example 2:
Input: nums = [10,20,10,30,30,20,40,50]
Output: [40,50]
Example 3:
Input: nums = [7,9]
Output: [7,9]
+ 10 hidden test cases run on Submit.
Constraints:
- ●
2 ≤ nums.length ≤ 3 × 10⁴ - ●
-3 × 10⁴ ≤ nums[i] ≤ 3 × 10⁴ - ●
Exactly two distinct values in nums occur an odd number of times; every other value occurs an even number of times
nums =
[4, 2, 4, 5, 2, 3, 3, 1]