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
|
901 Discussions
|
Please Login in order to post a comment
Simply XOR should solve this problem with just O(n) time complexity and O(1) space complexity.
num = 0 for i in a: num ^=i return num
Py solution :
Here is a solution with C# public static int lonelyinteger(List a) { Dictionary myDict = new Dictionary(a.Count); int result = 0; for(int i = 0; i < a.Count; i++){ if(myDict.ContainsKey(a[i])){ myDict[a[i]] += 1; }else{ myDict.Add(a[i], 1); } } foreach(KeyValuePair item in myDict ){ Console.WriteLine(item.Value+ " "); if(item.Value == 1){ result = item.Key; break; } } return result; }
}
I wish I had thought of the mathematical approach.