Problem: Single Number II (LeetCode #137)
Approach:
In this solution, we use an unordered_map (HashMap) approach to find the element that appears exactly once in the array.
The idea is:
Traverse the array and count the frequency of each element
Store frequencies using unordered_map
Most elements appear three times
The unique element appears only once
Traverse the frequency map and return the element with frequency 1
Steps:
1. Create an unordered_map to store frequencies
2. Traverse the array and count occurrences of each element
3. Traverse the frequency map
4. Check if frequency is equal to 1
5. Return the corresponding element
6. Return -1 as a fallback
Time Complexity:
O(n)
Space Complexity:
O(n)
GitHub:
https://github.com/Nitishkumar0517768
LeetCode Profile:
https://leetcode.com/u/Nitishkumar_05/
LinkedIn:
/ nitish-kumar-03a34a3a1
Notes:
Beginner-friendly HashMap approach
Easy to understand and implement
Uses frequency counting to identify the unique element
The optimal solution uses bit manipulation with O(1) extra space, but this approach is simpler for learning
#leetcode #cpp #coding #dsa #programming #codecraft #datastructures #datastructureandalgorithm