| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1373 人关注过本帖
标题:c#中找出数组中出现次数最多的元素
只看楼主 加入收藏
枫落de夜
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2013-4-11
结帖率:33.33%
收藏
已结贴  问题点数:20 回复次数:9 
c#中找出数组中出现次数最多的元素
c#中找出数组中出现次数最多的元素 求各种方法,还有详细的代码
搜索更多相关主题的帖子: 元素 
2013-04-11 16:02
yhlvht
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:36
帖 子:707
专家分:4405
注 册:2011-9-30
收藏
得分:10 
static void Main(string[] args)
{
    string[] str = new string[] { "f", "dd", "ff", "f", "asd", "dd", "dd", "g", "e", "f", "g", "f", "dd", "bs", "dd", "dd", "g", "e", "f", "g", "f", "dd" };
    Array.Sort(str);
    List<string> list = str.ToList<string>();
    //Hashtable ht = new Hashtable();
    while (list.Count > 0)
    {
        IEnumerable<string> str1 = list.Where(list1 => list1 == list[0]);
        //ht.Add(list[0], str1.Count<string>());
         Console.WriteLine(list[0] + "=" + str1.Count<string>());
        list.RemoveRange(0, str1.Count<string>());
    }
    Console.ReadKey();
}
2013-04-11 19:17
yhlvht
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:36
帖 子:707
专家分:4405
注 册:2011-9-30
收藏
得分:0 
随便想了一种方法,将就看吧
2013-04-11 19:19
lantian8134
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:8
帖 子:115
专家分:789
注 册:2013-1-11
收藏
得分:0 
string[] arr = { "b","a", "c", "a" };
            Dictionary<string, int> rs = new Dictionary<string, int>();
            foreach (string s in arr)
            {
                if (rs.ContainsKey(s))
                    rs[s] += 1;
                else
                    rs[s] = 1;
            }
            var r = rs.OrderByDescending(x => x.Value).First();
            Console.WriteLine(r.Key + " " + r.Value);
            Console.ReadKey();
2013-04-12 13:45
yhlvht
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:36
帖 子:707
专家分:4405
注 册:2011-9-30
收藏
得分:0 
回复 4楼 lantian8134
打印一个是不行的,因为可能有多个元素的个数都相同
一开始我也想用foreach或for,后来想要是元素特别多,就麻烦了,才搞成那样奇怪的代码了
2013-04-12 13:59
枫落de夜
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2013-4-11
收藏
得分:0 
回复 5楼 yhlvht
我是个新手,那个我没怎么看懂,我写的代码是随机生成一些数,找出最大最小值,并找出出现次数最多的元素,希望能能用一些简单的算法解决,这是我写的代码
int[] n = new int[10000];
            int min = 99999;
            int max = 0;
            System.Random rnd = new System.Random();
            for (int ctr = 0; ctr < 10000; ctr++)
            {
                n[ctr] = (int)rnd.Next(10000, 99999);
                Console.WriteLine("第{0}个数,值是{1}", ctr + 1,n[ctr]);
            }
            for (int ctr2 = 0; ctr2 < 10000; ctr2++)
            {
                if (n[ctr2] < min)
                {
                    min = n[ctr2];
                }
                if (n[ctr2] > max)
                {
                    max = n[ctr2];
                }
            }
        
            Console.WriteLine("最小数是{0},最大数是{1}", min, max);
            Console.ReadKey();
2013-04-12 14:25
枫落de夜
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2013-4-11
收藏
得分:0 
回复 4楼 lantian8134
我想用一些算法能不能解决这个问题,比如说从新在建一个数组,在遍历一次,但是那些算法我不会,这是我写的代码
int[] n = new int[10000];
            int min = 99999;
            int max = 0;
            System.Random rnd = new System.Random();
            for (int ctr = 0; ctr < 10000; ctr++)
            {
                n[ctr] = (int)rnd.Next(10000, 99999);
                Console.WriteLine("第{0}个数,值是{1}", ctr + 1,n[ctr]);
            }
            for (int ctr2 = 0; ctr2 < 10000; ctr2++)
            {
                if (n[ctr2] < min)
                {
                    min = n[ctr2];
                }
                if (n[ctr2] > max)
                {
                    max = n[ctr2];
                }
            }
        
            Console.WriteLine("最小数是{0},最大数是{1}", min, max);
            Console.ReadKey();
2013-04-12 14:36
lantian8134
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:8
帖 子:115
专家分:789
注 册:2013-1-11
收藏
得分:10 
回复 5楼 yhlvht
你说的没错,确实存在会有多条数据。
考虑的没你仔细呀!
string[] arr = { "b", "a", "c", "a", "c" };
            Dictionary<string, int> rs = new Dictionary<string, int>();
            foreach (string s in arr)
            {
                if (rs.ContainsKey(s))
                    rs[s] += 1;
                else
                    rs[s] = 1;
            }
            var r = rs.OrderByDescending(x => x.Value).First();
            var m = rs.Where(x => x.Value == r.Value);
            foreach (var n in m)
                Console.WriteLine(n.Key + " " + n.Value);
            
            Console.ReadKey();
2013-04-12 15:58
yhlvht
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:36
帖 子:707
专家分:4405
注 册:2011-9-30
收藏
得分:0 
给你个算法的
 int[] n = new int[10000];
            int min = 99999;
            int max = 0;
            System.Random rnd = new System.Random();
            for (int ctr = 0; ctr < 10000; ctr++)
            {
                n[ctr] = (int)rnd.Next(10000, 99999);
                Console.WriteLine("第{0}个数,值是{1}", ctr + 1, n[ctr]);
            }
            for (int ctr2 = 0; ctr2 < 10000; ctr2++)
            {
                if (n[ctr2] < min)
                {
                    min = n[ctr2];
                }
                if (n[ctr2] > max)
                {
                    max = n[ctr2];
                }
            }

            //------------排序----------------
            int i,j,temp;
            for (i = 0; i < n.Length - 1; i++)
            {
                for (j = i + 1; j < n.Length; j++)
                {
                    if (n[i] > n[j])
                    {
                        temp = n[i];
                        n[i] = n[j];
                        n[j] = temp;
                    }
                }
            }
            //----------查找出现次数最多的元素------------------
            int[] arrMax = new int[n.Length];//用于存放出现次数最多的元素
            int count = 1;  //记录出现最多的次数,初始为1,因为最少出现了1次
            int now = 1;    //临时记录每个数出现的次数
            int flag = 0;   //用于控制max数组的下标
            
            for (i = 0; i < n.Length - 1; i++)
            {
                if (n[i] == n[i + 1])
                {
                    now++;
                }
                else
                {
                    if (now == count)
                    {
                        arrMax[flag] = n[i];
                        flag++;
                    }
                    else if (now > count)
                    {
                        count = now;
                        arrMax = new int[n.Length - i];
                        flag = 0;
                        arrMax[flag] = n[i];
                        flag++;
                    }
                    now = 1;
                }
            }
            if (count == 1)
            {
                arrMax[flag] = n[n.Length - 1];
            }
            Console.WriteLine("最小数是{0},最大数是{1}", min, max);
            Console.WriteLine("出现次数最多的数为:");
            for (i = 0; i < arrMax.Length; i++)
            {
                if (arrMax[i] == 0)
                {
                    break;
                }
                else
                {
                    Console.WriteLine(arrMax[i]);
                }
            }
            Console.WriteLine("出现的次数为:" + count);
            Console.ReadKey();
2013-04-12 16:40
枫落de夜
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2013-4-11
收藏
得分:0 
回复 9楼 yhlvht
我试过了,可以的,十分感谢
2013-04-13 17:07
快速回复:c#中找出数组中出现次数最多的元素
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017807 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved