🧠
Brute Force (Hash Map Counting)
💡 This approach uses a hash map to count occurrences of each number. It's straightforward and helps beginners understand the problem but is not optimal in space.
Intuition
Count how many times each number appears, then find the one that appears once.
Algorithm
- Initialize an empty hash map to store counts.
- Iterate through the array, incrementing the count for each number.
- Iterate through the hash map to find the number with count 1.
- Return that number.
💡 This approach is easy to understand but uses extra space proportional to input size, which is not ideal for large datasets.
from collections import Counter
def singleNumber(nums):
counts = Counter()
for num in nums:
counts[num] += 1
for num, count in counts.items():
if count == 1:
return num
# Driver code
if __name__ == '__main__':
print(singleNumber([2,2,3,2])) # Output: 3
Line Notes
from collections import CounterImports Counter for easy frequency counting
counts = Counter()Initialize counter to store frequencies
for num in nums:Iterate over each number to count occurrences
if count == 1:Identify the unique number appearing once
import java.util.HashMap;
import java.util.Map;
public class Solution {
public static int singleNumber(int[] nums) {
Map<Integer, Integer> counts = new HashMap<>();
for (int num : nums) {
counts.put(num, counts.getOrDefault(num, 0) + 1);
}
for (Map.Entry<Integer, Integer> entry : counts.entrySet()) {
if (entry.getValue() == 1) {
return entry.getKey();
}
}
return -1; // Should never reach here
}
public static void main(String[] args) {
System.out.println(singleNumber(new int[]{2,2,3,2})); // Output: 3
}
}
Line Notes
Map<Integer, Integer> counts = new HashMap<>()Create map to store counts
for (int num : nums)Loop through array to count each number
counts.put(num, counts.getOrDefault(num, 0) + 1)Increment count for current number
if (entry.getValue() == 1)Find the number that appears once
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
int singleNumber(vector<int>& nums) {
unordered_map<int, int> counts;
for (int num : nums) {
counts[num]++;
}
for (auto& p : counts) {
if (p.second == 1) return p.first;
}
return -1; // Should never reach here
}
int main() {
vector<int> nums = {2,2,3,2};
cout << singleNumber(nums) << endl; // Output: 3
return 0;
}
Line Notes
#include <unordered_map>Include hash map for frequency counting
unordered_map<int, int> counts;Map to store counts of each number
for (int num : nums)Iterate over input array
if (p.second == 1) return p.first;Return the unique number
function singleNumber(nums) {
const counts = new Map();
for (const num of nums) {
counts.set(num, (counts.get(num) || 0) + 1);
}
for (const [num, count] of counts.entries()) {
if (count === 1) return num;
}
return -1; // Should never reach here
}
// Test
console.log(singleNumber([2,2,3,2])); // Output: 3
Line Notes
const counts = new Map()Initialize map to count frequencies
for (const num of nums)Iterate over each number in array
counts.set(num, (counts.get(num) || 0) + 1)Increment count for current number
if (count === 1) return numReturn the unique number found
We iterate through the array once to count frequencies and once over the map to find the unique number. The map can grow up to size n in worst case.
💡 For n=100,000, this means about 200,000 operations and memory proportional to n, which can be large.
Interview Verdict: Accepted but not optimal
This approach works but uses extra memory, which is not ideal for large inputs or strict space constraints.