int [] ss={1,2,5,2,7,5,2};
用。net技术取出数组里出现次数做多的这个数和它出现的次数

解决方案 »

  1.   


    int[] A = new int[9] { 1, 5, 2, 1, 2, 6, 5, 2, 5 };
                int[] B = new int[A.Length];   //记录每个数字重复的次数
                int[] max = new int[A.Length]; //记录出现次数最大的数,如果有多个,都列出来(比如2和5都出现了最多次--3次)            //将每个数字出现的次数计入B[]
                for (int i = 0; i < A.Length; i++)
                {
                    int m = 0;
                    for (int j = 0; j < A.Length; j++)
                    {
                        if (i != j)
                        {
                            if (A[i] == A[j])
                            {
                                m++;
                            }
                        }
                    }
                    B[i] = m;
                }
                //获取出现最多的次数
                int n = 0;
                for (int k = 0; k < B.Length - 1; k++)
                {
                    if (B[k] >= B[k + 1])
                    {
                        B[k + 1] = B[k];
                    }
                    n = B[k + 1];
                }
                //找出最大数存入max[]
                for (int i = 0; i < A.Length; i++)
                {
                    int m = 0;
                    for (int j = 0; j < A.Length; j++)
                    {
                        if (i != j)
                        {
                            if (A[i] == A[j])
                            {
                                if (i < j)
                                {
                                    m++;
                                }
                            }
                        }
                    }
                    if (m == n)
                    {
                        max[i] = A[i];
                        //将结果以弹出窗口的形式逐个输出
                        ClientScript.RegisterStartupScript(GetType(), "" + i, "<script>alert('" + max[i] + "')</script>");
                    }
                }
    百度找的:http://hi.baidu.com/zfy0921/blog/item/82b3912d5ac818e58a1399e7.html
    希望对你有用
      

  2.   

    可以使用Dictionary<int,int> 来搞定
      

  3.   

     static void Main(string[] args)
            {
                int[] ss ={ 1, 2, 5, 2, 7, 5, 2 }; 
                int max = -1;
                Dictionary<int, int> list = new Dictionary<int, int>();
                for (int i = 0; i < ss.Length; i++)
                {
                    if (list.ContainsKey(ss[i]))
                    {
                        list[ss[i]]++;
                        if (list[ss[i]] > max) max = ss[i];
                    }
                    else list.Add(ss[i], 1);
                }
                Console.WriteLine("{0} {1}",max,list[max]);           
            }
      

  4.   


                int[] ss = { 1,2,3,4,2,1,4,5,6,6,2,1,2,3,2};
                IEnumerable<IGrouping<int,int>> temp = ss.GroupBy(a => a);            int temp1 = temp.Max(a=>a.Count());            Console.Write("最大个数为"+temp1);
      

  5.   

    一句话怎么select出出现次数最多的数的集合
      

  6.   

    貌似linq挺好  9楼      学习了
      

  7.   

    如果有相同个数的数据怎么取?例如有3个2  和3个5,你要的是哪一个,还是都要        //7楼用Dictionary不错    
            private void button1_Click(object sender, EventArgs e)
            {
                int[] sum = new int[9] { 1, 5, 2, 1, 2, 6, 5, 2, 5 };
                ArrayList sumA = new ArrayList(sum);
                int maxSum=Convert.ToInt32(sumA[0]);//出现最多的那个数字
                int nCount=1;//出现的次数
                int tmpSum=0;
                int tmpCount=0;
                sumA.Sort();
                for (int i = 1; i < sumA.Count;i++)
                {
                    if (Convert.ToInt32(sumA[i]) == maxSum)
                    {
                        nCount = nCount + 1;
                    }
                    else 
                    {
                        if(nCount>tmpCount)
                        {
                            tmpCount = nCount;
                            tmpSum = maxSum;
                        }
                        nCount = 1;
                        maxSum =Convert.ToInt32(sum[i]);
                    }
                }
                if(nCount>tmpCount)
                {
                    tmpCount = nCount;
                    tmpSum = maxSum;
                }
                Console.WriteLine("{0}  {1}",tmpSum,tmpCount);
            }
      

  8.   

    为了一行代码疯掉了int[] input = { 5, 1, 1, 1, 3, 3, 2, 2, 2, 4, 4 };
    var query = input.GroupBy(n => n).GroupBy(n => n.Count()).OrderByDescending(n => n.Key).Select(n => new { count = n.Key, nums = n.Select(o => o).Select(p => p.Key).ToArray() });
      

  9.   

                int[] ss = { 1, 2, 3, 4, 2, 1, 4, 5, 6, 6, 2, 1, 2, 3, 2 };
                var sub =(
                    from b in
                        (
                        from a in ss
                        group a by a into g
                        select new { k = g.Key, c = g.Count() }
                        )
                    orderby b.c descending
                    select new { k=b.k, c=b.c }).Take(1).ToArray();            var temp1 = sub[0].k;
                var temp2 = sub[0].c;
    http://www.mystruggle.com.cn
      

  10.   

                int[] ss = { 1, 2, 3, 4, 2, 1, 4, 5, 6, 6, 2, 1, 2, 3, 2 };
                int k =(
                    from b in
                        (
                        from a in ss
                        group a by a into g
                        select new { k = g.Key, c = g.Count() }
                        )
                    orderby b.c descending
                    select new { k=b.k, c=b.c }).Take(1).ToArray()[0].k;
    http://www.mystruggle.com.cn