We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Lonely Integer
- Discussions
Lonely Integer
Lonely Integer
Sort by
recency
|
232 Discussions
|
Please Login in order to post a comment
yo guys this is simpler and more effecient.....
from collections import Counter as c
at #write your code replace it with res=c(a) for i in a: if res[i]==1: return i
The solution to the problem is by summing xor values;
** int lonelyinteger(vector a) { map freq; for(int i=0;i for(auto i:freq) { if(i.second == 1) { return (i.first); }
} **
Save time and space I believe
int result = 0; Map count = new HashMap<>();
for(int num : a) { if(count.containsKey(num)) { count.remove(num); } else { count.put(num, 0); } } for(Map.Entry entry : count.entrySet()) { result = entry.getKey(); }
// Using map --> create map // key--> item, value --> occurences of item in input array (a) // iterate through map --> return key whose value is 1