Dictionary find duplicate values
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
class Program { static void Main(string[] args) { //initialize a dictionary with keys and values. Dictionary<int, string> birds = new Dictionary<int, string>() { {1,"Golden Pheasant"}, {2,"Southern Screamer"}, {3,"Golden Pheasant"}, {4,"Swan Goose"}, {5,"Swan Goose"}, {6,"Golden Pheasant"}, {7,"Greylag Goose"} }; Console.WriteLine( "dictionary elements.........."); foreach (KeyValuePair<int, string> pair in birds) { Console.WriteLine(pair.Key + " ........ " + pair.Value); } //get dictionary duplicate values. var duplicatesValue = birds.GroupBy(x => x.Value).Where(x => x.Count() > 1); Console.WriteLine("\ndictionary duplicate values..........<br />"); foreach (var item in duplicatesValue) { Console.WriteLine(item.Key ); } Console.ReadLine(); } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
dictionary elements.......... 1 ........ Golden Pheasant 2 ........ Southern Screamer 3 ........ Golden Pheasant 4 ........ Swan Goose 5 ........ Swan Goose 6 ........ Golden Pheasant 7 ........ Greylag Goose dictionary duplicate values..........<br /> Golden Pheasant Swan Goose |