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.
Example 1:
Input: nums = [2,2,3,2] Output: 3
Example 2:
Input: nums = [0,1,0,1,0,1,99] Output: 99
Constraints:
1 <= nums.length <= 3 * 104-231 <= nums[i] <= 231 - 1nums appears exactly three times except for one element which appears once.Problem Overview: You are given an integer array where every element appears exactly three times except for one element that appears once. Your task is to find that unique element while maintaining linear time complexity and minimal extra space.
Approach 1: Use a Map to Count Frequencies (O(n) time, O(n) space)
The most direct solution is counting how many times each value appears. Iterate through the array and store frequencies in a hash map where the key is the number and the value is its count. After building the map, iterate through its entries and return the number whose frequency equals one. Hash map lookup and updates run in constant time on average, so the full traversal stays O(n).
This approach works well when clarity is more important than memory usage. It is easy to implement in any language and clearly reflects the problem statement: count occurrences and pick the unique one. The tradeoff is extra memory proportional to the number of distinct values. Problems involving counting frequencies commonly rely on structures like hash maps and appear frequently with array processing tasks.
Approach 2: Bit Manipulation (O(n) time, O(1) space)
The optimal solution uses bit manipulation. Instead of counting whole numbers, count how many times each bit position is set across all elements. For a 32‑bit integer, iterate through all 32 bit positions. For each position, scan the array and count how many numbers have that bit set. Since every repeated number appears exactly three times, the total count for those bits will always be divisible by three.
Take the count modulo 3 for each bit. If the remainder is 1, that bit belongs to the unique number. Reconstruct the final answer by setting those bits in the result. This works because the contributions from numbers appearing three times cancel out under modulo 3 arithmetic.
This method runs in O(32 × n) time, which simplifies to O(n), and uses constant extra space. It relies heavily on operations like bit shifts, bit masks, and bitwise AND. These techniques are core concepts in bit manipulation problems and often appear when constraints forbid extra memory.
Recommended for interviews: The bit manipulation approach is what most interviewers expect. The frequency map solution demonstrates that you understand the problem and can produce a correct baseline quickly. However, the O(1) space bit counting technique shows deeper understanding of binary representation and optimization. Interviewers often use this problem specifically to test your ability to reason about bits and eliminate repeated patterns using modulo arithmetic.
This approach uses bit manipulation to solve the problem with constant space and linear runtime. The idea is to count the number of times each bit is set in the given numbers modulo 3 using two integers. This effectively filters out the bits that appear in numbers repeated thrice and leaves the bits of the unique number.
In this code, we maintain two variables ones and twos to track the bits' appearances. When a bit appears the first time, it will be set in ones. If it appears the second time, it moves to twos while being removed from ones. When it appears the third time, it is cleared from both ones and twos using a common mask. This helps in removing all bits that occur thrice, hence leaving behind the unique element.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n) - where n is the number of elements in the array.
Space Complexity: O(1) - as only a fixed amount of space is used regardless of the input size.
This approach involves using a data structure to count occurrences of each number. Although this uses linear time complexity, it requires additional memory to store frequencies, which violates the constant space constraint.
This implementation sorts the array first, which costs O(n log n) time, and then iterates over it checking for numbers that do not repeat thrice by comparison. Although simple, this technique also requires modification of the input if sorting is destructive.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n log n) - primarily due to the sorting step.
Space Complexity: O(1) - if in-place sort is assumed.
| Approach | Complexity |
|---|---|
| Bit Manipulation | Time Complexity: O(n) - where n is the number of elements in the array. |
| Use a Map to Count Frequencies | Time Complexity: O(n log n) - primarily due to the sorting step. |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Map to Count Frequencies | O(n) | O(n) | Best for readability or quick implementation when memory constraints are not strict |
| Bit Manipulation (Bit Count mod 3) | O(n) | O(1) | Optimal solution when the problem requires constant extra space |
Single Number - Leetcode 136 - Python • NeetCode • 117,983 views views
Watch 9 more video solutions →Practice Single Number II with our built-in code editor and test cases.
Practice on FleetCode