Single Number

Monisha Mathew
1 min readMay 30, 2019

--

Question: Given a non-empty array of integers, every element appears twice except for one. Find that single one.

You may view the full question here.

Approach 1: Here is an approach using HashMaps —

//Approach 1:
//Runtime: 7ms
//Memory usage: 38.6MB
class Solution {
public int singleNumber(int[] nums) {
HashMap<Integer, Integer> map = new HashMap();
for(int n : nums){
if(map.containsKey(n)){
map.remove(n);
} else {
map.put(n, 1);
}
}
for(int key : map.keySet()){
return key;
}
return 0;
}

}

Find more posts here.

Cheers & Chao!

--

--