Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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# 获取顶部x相等元素的索引_C#_Linq - Fatal编程技术网

C# 获取顶部x相等元素的索引

C# 获取顶部x相等元素的索引,c#,linq,C#,Linq,我需要得到整数排序列表中所有最大元素的索引,这些元素彼此相等 因此,考虑到这个列表 elements: {1 , 1, 2, 3, 4, 4, 5, 6, 7, 7, 8, 9, 10, 11, 11, 12, 13, 13, 13} index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

我需要得到整数排序列表中所有最大元素的索引,这些元素彼此相等

因此,考虑到这个列表

elements:   {1 , 1, 2, 3, 4, 4, 5, 6, 7, 7, 8, 9, 10, 11, 11, 12, 13, 13, 13}
index:       0   1  2  3  4  5  6  7  8  9  10 11 12  13  14  15  16  17  18
                                                                  ^   ^   ^
我将得到这个输出

{16,17,18}
到目前为止,我已经

list.Select((x, i) => new {x, i})
获取索引,但我不能将OrderBy与First或Single一起使用,因为我需要所有max元素的索引,而不仅仅是最上面的索引

有没有一种优雅的方法可以通过LINQ或其他方式实现这一点?

简单/懒惰的方法:

        var a = new[] {1, 1, 2, 3, 4, 4, 5, 6, 7, 7, 8, 9, 10, 11, 11, 12, 13, 13, 13};
        var b = new List<int>();
        var max = a.Max();
        for (var i = 0; i < a.Length; i++)
        {
            if (a[i] == max) b.Add(i);
        }

这样就可以对项目进行排序,您只需获取第一个项目的索引,该项目的最大值将与最后一个项目的值完全相同,然后创建从该索引到列表末尾的索引范围:

var items = new List<int> {1,1,2,3,4,4,5,6,7,7,8,9,10,11,11,12,13,13,13};
int startIndex = items.IndexOf(items[items.Count - 1]);
var indexes = Enumerable.Range(startIndex, items.Count - startIndex);

我不会使用LINQ,因为它是一个简单的集合

//var data = new[] {1, 1, 13, 2, 3, 4, 4, 5, 6, 7, 7, 8, 9, 10, 11, 11, 12, 13, 13, 13};
var data = new[] {1, 1, 2, 3, 4, 4, 5, 6, 7, 7, 8, 9, 10, 11, 11, 12, 13, 13, 13};

var largest = int.MinValue;
var indices = new List<int>();

foreach (var x in data.Select((value, idx) => new {value, idx}))
{
    if (x.value > largest)
    {
        indices.Clear();
        largest = x.value;
    }

    // if unsorted
    //if (x.value == largest) indices.Add(x.idx);

    // if sorted you don't need to check against largest
    indices.Add(x.idx);
}

Console.WriteLine("largest = {0}; indices = {1}", largest, string.Join(", ", indices));

这将为您提供所有元素的结果,这些元素都具有重复的索引:

var result = elements.Select((value, index) => new { value, index })
             .Where(g => elements.FindAll(v => v == g.value).Count > 1)
             .GroupBy((a) => a.value).OrderByDescending((g) => g.Key).Take(3);
            //I placed Take(3) as example since you said you need to find
            //elements who are equal to each other,so only those that are
            // not distinct(have duplicates) get into the collection.

            //this will loop through the results and display the value(the item
            //on the list) and its respective index.
            foreach (var item in result.SelectMany(g => g))
            {
                string outcome = item.value + " - " + item.index;
                Console.WriteLine(outcome);
            }

你说的顶级元素是什么意思?你尝试过什么?它保证像示例一样排序吗?为什么你认为这是LINQ的一个好例子?这将计算循环每次迭代的最大值。这比我尝试的任何方法都更有意义。非常感谢。
var result = elements.Select((value, index) => new { value, index })
             .Where(g => elements.FindAll(v => v == g.value).Count > 1)
             .GroupBy((a) => a.value).OrderByDescending((g) => g.Key).Take(3);
            //I placed Take(3) as example since you said you need to find
            //elements who are equal to each other,so only those that are
            // not distinct(have duplicates) get into the collection.

            //this will loop through the results and display the value(the item
            //on the list) and its respective index.
            foreach (var item in result.SelectMany(g => g))
            {
                string outcome = item.value + " - " + item.index;
                Console.WriteLine(outcome);
            }