Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何遍历数组并创建一个列表来计算一个数字的重复次数_C#_Arrays_List_Replace_Tuples - Fatal编程技术网

C# 如何遍历数组并创建一个列表来计算一个数字的重复次数

C# 如何遍历数组并创建一个列表来计算一个数字的重复次数,c#,arrays,list,replace,tuples,C#,Arrays,List,Replace,Tuples,论坛之友 首先,我为我的英语道歉 如何遍历一个数组并创建一个列表来计算一个数字的重复次数,并按item2从高到低排序 我有以下数组: int[] arrayNumeros= new int[10] {1,2,3,4,5,6,1,1,2,3} 我有以下清单: List<Tuple<int, int>> contadorNumerosTotal = new List<Tuple<int, int>>(); 非常感谢您抽出时间以下内容将产生预期的结果

论坛之友

首先,我为我的英语道歉

如何遍历一个数组并创建一个列表来计算一个数字的重复次数,并按item2从高到低排序

我有以下数组:

int[] arrayNumeros= new int[10] {1,2,3,4,5,6,1,1,2,3}
我有以下清单:

List<Tuple<int, int>> contadorNumerosTotal = new List<Tuple<int, int>>();

非常感谢您抽出时间

以下内容将产生预期的结果:

int[] numbers = new int[] {1,2,3,4,5,6,1,1,2,3};

Dictionary<int, int> numberCount = new Dictionary<int, int>();

int len = numbers.Length;
for(int i = 0; i < len; i++)
{
    if (numberCount.ContainsKey(numbers[i]))
    {
        numberCount[numbers[i]]++;
    }
    else
    {
        numberCount[numbers[i]] = 1;
    }
}

// Sort by value if necessary. Otherwise loop through numberCount.
var sortednumberCount = numberCount.OrderByDescending(pair => pair.Value);
foreach (var number in sortednumberCount)
{
    Console.WriteLine("{0} -> {1}", number.Key, number.Value);
}

您可以使用以下内容:

List<Tuple<int, int>> contadorNumerosTotal =
    arrayNumeros.GroupBy (i => i).Select (ints => new Tuple<int, int> (ints.Key, ints.Count ())).ToList ();
contadorNumerosTotal.Sort ((tuple, tuple1) => tuple1.Item2.CompareTo (tuple.Item2));
int [] arrayNumeros = new int[10] {1, 2, 3, 4, 5, 6, 1, 1, 2, 3};
List<Tuple<int, int>> contadorNumerosTotal =
    arrayNumeros.GroupBy (i => i).Select (ints => new Tuple<int, int> (ints.Key, ints.Count ())).ToList ();
contadorNumerosTotal.Sort ((tuple, tuple1) => tuple1.Item2.CompareTo (tuple.Item2));
foreach (var count in contadorNumerosTotal)
    Console.WriteLine ($"{count.Item1} -> {count.Item2}");
Console.ReadLine ();
其结果如下:

1 -> 3
2 -> 2
3 -> 2
4 -> 1
5 -> 1
6 -> 1
阅读有关groupBy的更多信息


使用Linq GROUPBY可能的重复可以查看使用线性搜索算法,如果您的目标只是找到重复的元素很多感谢Tristan Gibson。这正是我所需要的。一种问候语。然而,这种问候语的类型与他想要的不同。顺便说一句,解释也没什么坏处。
List<Tuple<int, int>> contadorNumerosTotal =
    arrayNumeros.GroupBy (i => i).Select (ints => new Tuple<int, int> (ints.Key, ints.Count ())).ToList ();
contadorNumerosTotal.Sort ((tuple, tuple1) => tuple1.Item2.CompareTo (tuple.Item2));
int [] arrayNumeros = new int[10] {1, 2, 3, 4, 5, 6, 1, 1, 2, 3};
List<Tuple<int, int>> contadorNumerosTotal =
    arrayNumeros.GroupBy (i => i).Select (ints => new Tuple<int, int> (ints.Key, ints.Count ())).ToList ();
contadorNumerosTotal.Sort ((tuple, tuple1) => tuple1.Item2.CompareTo (tuple.Item2));
foreach (var count in contadorNumerosTotal)
    Console.WriteLine ($"{count.Item1} -> {count.Item2}");
Console.ReadLine ();
1 -> 3
2 -> 2
3 -> 2
4 -> 1
5 -> 1
6 -> 1
int[] arrayNumeros= new int[10] {1,2,3,4,5,6,1,1,2,3};

var result = arrayNumeros
    .GroupBy( e => e )
    .ToDictionary( e => e.Key, e => e.Count() )
    .OrderByDescending( e => e.Value )
    .ThenBy( e => e.Key )
    .Select( e => Tuple.Create( e.Key, e.Value ) )
    .ToList();