Single Number II
Expert Answer & Key Takeaways
A complete guide to understanding and implementing Bit Manipulation.
Single Number II
Given an integer array
nums where every element appears three times except for one, which appears exactly once. Find the single element and return it.You must implement a solution with a linear runtime complexity and use only constant extra space.
Examples
Input: nums = [2,2,3,2]
Output: 3
Input: nums = [0,1,0,1,0,1,99]
Output: 99
Approach 1
Level I: Brute Force (Frequency Map)
Intuition
The simplest way to track occurrences is using a hash map or frequency array. We count how many times each number appears and return the one with a count of 1.
⏱ $O(N)$💾 $O(N)$
Approach 2
Level II: Sorting
Intuition
If we sort the numbers, identical numbers will be adjacent. We can jump in steps of 3 and check if
nums[i] is different from nums[i+1].⏱ $O(N \log N)$💾 $O(1)$ (or $O(N)$ depending on sort implementation)
Approach 3
Level III: Optimal (Bit Manipulation)
Intuition
Every number that appears three times will contribute exactly 3 (or 0) to the sum of bits at any given position. If we sum the bits at each position and take modulo 3, the remaining bit belongs to the single number.
⏱ $O(N)$💾 $O(1)$
Course4All Technical Board
Verified ExpertSenior Software Engineers & Algorithmic Experts
Our DSA content is authored and reviewed by engineers from top tech firms to ensure optimal time and space complexity analysis.
Pattern: 2026 Ready
Updated: Weekly
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.