我正在尝试在整数数组中查找最大值的出现。
例如
int[] ar = [3, 1, 2, 3];
此处,最大3
重复两次,因此预期输出为2
。
这有效,我得到的计数为2
,因为最大值3
在数组中出现了两次
var max = int.MinValue;
var occurrenceCount = 0;
foreach(var x in ar)
{
if (x >= max) max = x;
}
foreach(var x in ar)
{
if (x == max) occurrenceCount++;
}
输出:2 // occurrenceCount
使用Linq
更简单,
var occurrenceCount = ar.Count(x => x == ar.Max())
输出:2 // occurrenceCount
现在没有Linq
了,是否有任何简化或有效的方式来做到这一点?
答案 0 :(得分:4)
至少,您可以合并两个第一个数组。我仍然会使用Linq解决方案。更清楚了。如果您真的想谈论性能,请先阅读Which is faster?。
这是O(n)解决方案:
int[] ar = {3, 1, 2, 3, 3, 4, 4};
int max = ar[0];
var occurrenceCount = 1;
for(var i = 1; i < ar.Length; i++)
{
if (ar[i] > max) {
max = ar[i];
occurrenceCount = 1;
}
else if (ar[i] == max) {
occurrenceCount++;
}
}
WriteLine(max);
WriteLine(occurrenceCount);
答案 1 :(得分:1)
我没有使用linq。我用过lamda:)
int[] ar = new[] { 3, 1, 2, 3 };
var result = ar.GroupBy(x => x) //values groups
.Select(x => new
{
Number = x.Key,
Count = x.Count()
}).OrderByDescending(x => x.Count) //Short
.FirstOrDefault(); //First Result
result.Count // how many
result.Key // max number
没有Linq和Lamda
int[] ar = new[] { 3, 1, 2, 3 };
Array.Sort(ar);
Array.Reverse(ar);
var maxValue = ar[0];
var occurrenceCount = 0;
foreach (var item in ar)
{
if (item == maxValue)
occurrenceCount++;
}
答案 2 :(得分:1)
基于Max的implementation和Enumerable上的GetCount,您可以通过在Max的foreach中添加一个测试来简单分解为因数,例如:
FormControl.setValue(obj)
最酷的部分是,很容易使它变得更像:
public static int CountMax(this IEnumerable<int> source)
{
if (source == null)
{
throw new ArgumentException();
}
int value = 0;
bool hasValue = false;
int count = 0;
foreach (int x in source)
{
if (hasValue)
{
if (x > value)
{
value = x;
count = 1;
}
else if (x == value)
{
count++;
}
}
else
{
value = x;
count = 1;
hasValue = true;
}
}
if (hasValue)
{
return count;
}
throw new Exception("no elements");
}
答案 3 :(得分:1)
您可以尝试更灵活的方法:
using System.Collections.Generic;
namespace ConsoleApp42
{
class Program
{
static void Main (string[] args)
{
var array = new int[] { 1, 2, 3, 1, 1, 4, 4, 4, 4, 1, 1, 1 };
//var array = new string[] { "a", "b", "a", "a" };
var result = array.MaxCount ();
}
}
public static class Extensions
{
public static (long count, T max) MaxCount<T> (this IEnumerable<T> source, IComparer<T> comparer = null)
{
if (comparer is null) comparer = Comparer<T>.Default;
(long count, T max) result = (0, default (T));
foreach (var element in source)
{
if (result.count == 0) // is first element?
{
result.max = element;
result.count = 1;
continue;
}
int compareResult = comparer.Compare (element, result.max);
if (compareResult == 0) // element == max
{
result.count++;
}
else if (compareResult > 0) // element > max
{
result.max = element;
result.count = 1;
}
}
return result;
}
}
}
答案 4 :(得分:1)
int[] list = new int[] { 1, 1, 2, 3, 6, 7, 6, 6, 6, 8, 9 };
Dictionary<int, int> occ = new Dictionary<int, int>();
for (int i = 0; i < list.Length; i++)
{
var val = list[i];
var count = 0;
for (int j = 0; j < list.Length; j++)
{
if (val == list[j])
{
count++;
}
}
occ.TryAdd(val, count);
}
var maxCount = occ.Values.Max();
var repNumber = occ.FirstOrDefault(x => x.Value == maxCount).Key;