136. Single Number
Approach 2: Hash Table
Algorithm
We use hash table to avoid the time required for searching the elements.
- Iterate through all elements in
nums
and set up key/value pair. - Return the element which appeared only once.
Implementation
class Solution {
public int singleNumber(int[] nums) {
HashMap<Integer, Integer> hash_table = new HashMap<>();
for (int i : nums) {
hash_table.put(i, hash_table.getOrDefault(i, 0) + 1);
}
for (int i : nums) {
if (hash_table.get(i) == 1) {
return i;
}
}
return 0;
}
}
Java
class Solution {
public int singleNumber(int[] nums) {
HashMap<Integer, Integer> hash_table = new HashMap<>();
for (int i : nums) {
hash_table.put(i, hash_table.getOrDefault(i, 0) + 1);
}
for (int i : nums) {
if (hash_table.get(i) == 1) {
return i;
}
}
return 0;
}
}
Python
from collections import defaultdict
class Solution:
def singleNumber(self, nums: List[int]) -> int:
hash_table = defaultdict(int)
for i in nums:
hash_table[i] += 1
for i in hash_table:
if hash_table[i] == 1:
return i
Complexity Analysis
- Time complexity : . Time complexity of
for
loop is . Time complexity of hash table (dictionary in python) operationpop
is . - Space complexity : . The space required by
hash_table
is equal to the number of elements innums
.