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.
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;
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Lonely Integer
You are viewing a single comment's thread. Return to all comments →
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; }
}