当前位置:编程学习 > C#/ASP.NET >>

查找数组中重复最多的项

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(); 

 


摘自 欧阳小燕

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,