24. Unique Number III
The problem can be found at the following link: 🔗 Question Link
🧩 Problem Description
Given an array arr[] where every element occurs thrice except one element which occurs only once, your task is to find that unique element.
📘 Examples
Example 1:
Input:
arr[] = [1, 10, 1, 1]
Output:
10
Explanation:
All numbers except 10 occur three times. Hence, the answer is 10.
Example 2:
Input:
arr[] = [3, 2, 1, 34, 34, 1, 2, 34, 2, 1]
Output:
3
Explanation:
Only 3 occurs once. All other numbers appear exactly three times.
🔒 Constraints
$( 1 \leq \text{arr.size()} \leq 10^5 )$
$( \text{arr.size()}$ % 3 = 1 )
$( -10^9 \leq \text{arr[i]} \leq 10^9 )$
✅ My Approach:
Optimized Bitwise Counting
This method uses bitwise manipulation to keep track of bits appearing once, twice, and thrice across the array.
Algorithm Steps:
Initialize two variables
onesandtwosto 0.For each element:
Update
onesas:(ones ^ num) & ~twosUpdate
twosas:(twos ^ num) & ~ones
After the loop,
oneswill hold the element that appears only once.
🧮 Time and Auxiliary Space Complexity
Expected Time Complexity: O(N), since we iterate once through the array.
Expected Auxiliary Space Complexity: O(1), as we only use a constant number of variables.
🧠 Code (C++)
⚡ Alternative Approaches
📊 2️⃣ Hash Map Frequency Count
Algorithm Steps:
Traverse the array and record each number’s count in an
unordered_map.Iterate over the map and return the key with frequency = 1.
📝 Complexity Analysis:
Time: O(n)
Space: O(n)
✅ Why This Approach?
Straightforward to implement
Handles any distribution of frequencies, not limited to exactly three repeats
📊 3️⃣ Sorting and Scan
Algorithm Steps:
Sort the array.
Scan with index
ifrom 0 to n−1 in steps of 3:If
arr[i] == arr[i+1] == arr[i+2], skip these three (i += 3);Else, return
arr[i].
If loop ends, the last element is the single one.
📝 Complexity Analysis:
Time: O(n log n)
Space: O(1) (in-place sort)
✅ Why This Approach?
No extra data structures beyond sorting
Intuitive when you can afford the sort overhead
🆚 Comparison of Approaches
Approach
⏱️ Time
🗂️ Space
✅ Pros
⚠️ Cons
Bitwise Counting
🟢 O(n)
🟢 O(1)
Fastest, constant space
Bitwise logic can be tricky
Hash Map Frequency
🟢 O(n)
🔴 O(n)
Easiest to implement
Extra memory for map
Sorting + Scan
🔴 O(n log n)
🟢 O(1)
No extra DS (in-place)
Slower due to sort
✅ Best Choice?
Scenario
Recommended Approach
✅ Exactly one unique, rest appear thrice
🥇 Bitwise Counting
✅ Quick implementation, input size moderate
🥈 Hash Map Frequency
✅ Memory very tight, sorting overhead acceptable
🥉 Sorting + Scan
🧑💻 Code (Java)
🐍 Code (Python)
🧠 Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: 📬 Any Questions?. Let’s make this learning journey more collaborative!
⭐ If you find this helpful, please give this repository a star! ⭐
📍Visitor Count
Last updated