Single Number III
Master this topic with zero to advance depth.
Single Number III
Given an integer array nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. You can return the answer in any order.
You must write an algorithm that runs in linear runtime complexity and uses only constant extra space.
Examples
Level I: Brute Force (Frequency Map)
Intuition
The most intuitive approach is to use a hash map to count the frequency of each number and extract those with a count of 1.
Level II: Sorting
Intuition
Sort the array to bring duplicates together. Iterate through the array; if current element is not equal to the next, it's a single number. Otherwise, skip two.
Level III: Optimal (Bitwise Partitioning)
Intuition
XORing all numbers gives . Find the lowest set bit in this XOR sum to divide all numbers into two groups (where this bit is set vs. not set). XORing within each group isolates and .
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.