查找数组中重复最多的项

string[] arr1 = {"广州", "中国", "上海", "北京","中国"};

            Dictionary<string ,int> d = new Dictionary<string,int>();

            foreach (var item in arr1)
            {
                if (d.ContainsKey(item))
                {
                   d[item]=d[item] + 1;
                }
                else
                {
                    d.Add(item, 1);
                }                
            }

            string s = d.OrderByDescending(p=>p.Value).First().Key;

            Console.WriteLine(s);
            Console.ReadKey();

你可能感兴趣的:(查找数组中重复最多的项)