如何获得Int数组中最常见的值? (C#)

时间:2022-01-21 21:28:55

How to get the most common value in an Int array using C#

如何使用C#获取Int数组中最常见的值

eg: Array has the following values: 1, 1, 1, 2

例如:Array具有以下值:1,1,1,2

Ans should be 1

Ans应为1

5 个解决方案

#1


27  

var query = (from item in array
        group item by item into g
        orderby g.Count() descending
        select new { Item = g.Key, Count = g.Count() }).First();

For just the value and not the count, you can do

对于价值而不是数量,你可以做到

var query = (from item in array
                group item by item into g
                orderby g.Count() descending
                select g.Key).First();

Lambda version on the second:

第二个Lambda版本:

var query = array.GroupBy(item => item).OrderByDescending(g => g.Count()).Select(g => g.Key).First();

#2


14  

Some old fashioned efficient looping:

一些老式的高效循环:

var cnt = new Dictionary<int, int>();
foreach (int value in theArray) {
   if (cnt.ContainsKey(value)) {
      cnt[value]++;
   } else {
      cnt.Add(value, 1);
   }
}
int mostCommonValue = 0;
int highestCount = 0;
foreach (KeyValuePair<int, int> pair in cnt) {
   if (pair.Value > highestCount) {
      mostCommonValue = pair.Key;
      highestCount = pair.Value;
   }
}

Now mostCommonValue contains the most common value, and highestCount contains how many times it occured.

现在,mostCommonValue包含最常见的值,而highestCount包含它发生的次数。

#3


1  

Maybe O(n log n), but fast:

也许是O(n log n),但很快:

sort the array a[n]

// assuming n > 0
int iBest = -1;  // index of first number in most popular subset
int nBest = -1;  // popularity of most popular number
// for each subset of numbers
for(int i = 0; i < n; ){
  int ii = i; // ii = index of first number in subset
  int nn = 0; // nn = count of numbers in subset
  // for each number in subset, count it
  for (; i < n && a[i]==a[ii]; i++, nn++ ){}
  // if the subset has more numbers than the best so far
  // remember it as the new best
  if (nBest < nn){nBest = nn; iBest = ii;}
}

// print the most popular value and how popular it is
print a[iBest], nBest

#4


1  

  public static int get_occure(int[] a)
    {
        int[] arr = a;
        int c = 1, maxcount = 1, maxvalue = 0;
        int result = 0;
        for (int i = 0; i < arr.Length; i++)
        {
            maxvalue = arr[i];
            for (int j = 0; j <arr.Length; j++)
            {

                if (maxvalue == arr[j] && j != i)
                {
                    c++;
                    if (c > maxcount)
                    {
                        maxcount = c;
                        result = arr[i];

                    }
                }
                else
                {
                    c=1;

                }

            }


        }
        return result;
    }

#5


1  

I know this post is old, but someone asked me the inverse of this question today.

我知道这篇文章很老了,但今天有人问我这个问题的反面。

LINQ Grouping

LINQ分组

sourceArray.GroupBy(value => value).OrderByDescending(group => group.Count()).First().First();

Temp Collection, similar to Guffa's:

Temp Collection,类似于Guffa的:

var counts = new Dictionary<int, int>();
foreach (var i in sourceArray)
{
    if (!counts.ContainsKey(i)) { counts.Add(i, 0); }
    counts[i]++;
}
return counts.OrderByDescending(kv => kv.Value).First().Key;

#1


27  

var query = (from item in array
        group item by item into g
        orderby g.Count() descending
        select new { Item = g.Key, Count = g.Count() }).First();

For just the value and not the count, you can do

对于价值而不是数量,你可以做到

var query = (from item in array
                group item by item into g
                orderby g.Count() descending
                select g.Key).First();

Lambda version on the second:

第二个Lambda版本:

var query = array.GroupBy(item => item).OrderByDescending(g => g.Count()).Select(g => g.Key).First();

#2


14  

Some old fashioned efficient looping:

一些老式的高效循环:

var cnt = new Dictionary<int, int>();
foreach (int value in theArray) {
   if (cnt.ContainsKey(value)) {
      cnt[value]++;
   } else {
      cnt.Add(value, 1);
   }
}
int mostCommonValue = 0;
int highestCount = 0;
foreach (KeyValuePair<int, int> pair in cnt) {
   if (pair.Value > highestCount) {
      mostCommonValue = pair.Key;
      highestCount = pair.Value;
   }
}

Now mostCommonValue contains the most common value, and highestCount contains how many times it occured.

现在,mostCommonValue包含最常见的值,而highestCount包含它发生的次数。

#3


1  

Maybe O(n log n), but fast:

也许是O(n log n),但很快:

sort the array a[n]

// assuming n > 0
int iBest = -1;  // index of first number in most popular subset
int nBest = -1;  // popularity of most popular number
// for each subset of numbers
for(int i = 0; i < n; ){
  int ii = i; // ii = index of first number in subset
  int nn = 0; // nn = count of numbers in subset
  // for each number in subset, count it
  for (; i < n && a[i]==a[ii]; i++, nn++ ){}
  // if the subset has more numbers than the best so far
  // remember it as the new best
  if (nBest < nn){nBest = nn; iBest = ii;}
}

// print the most popular value and how popular it is
print a[iBest], nBest

#4


1  

  public static int get_occure(int[] a)
    {
        int[] arr = a;
        int c = 1, maxcount = 1, maxvalue = 0;
        int result = 0;
        for (int i = 0; i < arr.Length; i++)
        {
            maxvalue = arr[i];
            for (int j = 0; j <arr.Length; j++)
            {

                if (maxvalue == arr[j] && j != i)
                {
                    c++;
                    if (c > maxcount)
                    {
                        maxcount = c;
                        result = arr[i];

                    }
                }
                else
                {
                    c=1;

                }

            }


        }
        return result;
    }

#5


1  

I know this post is old, but someone asked me the inverse of this question today.

我知道这篇文章很老了,但今天有人问我这个问题的反面。

LINQ Grouping

LINQ分组

sourceArray.GroupBy(value => value).OrderByDescending(group => group.Count()).First().First();

Temp Collection, similar to Guffa's:

Temp Collection,类似于Guffa的:

var counts = new Dictionary<int, int>();
foreach (var i in sourceArray)
{
    if (!counts.ContainsKey(i)) { counts.Add(i, 0); }
    counts[i]++;
}
return counts.OrderByDescending(kv => kv.Value).First().Key;