我如何在使用LINQ数组的最高值的指数? [英] How do I get the index of the highest value in an array using LINQ?

查看:85
本文介绍了我如何在使用LINQ数组的最高值的指数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有双打的阵列和我想要的最高值的索引。这些都是我想出来的迄今为止解决方案,但我认为必须有一个更好的解决方案。想法?

 双击[]分数=新的双[] {12.2,13.3,5,17.2,2.2,4.5};
INT topScoreIndex = score.Select((项目,INDX)=>新建项目{=项指数= INDX})OrderByDescending(X => x.Item)。选择(X => x.Index)。首先();topScoreIndex = score.Select((项目,INDX)=>新建项目{=项指数= INDX})排序依据(X => x.Item)。选择(X => x.Index)。去年( );双MAXVAL = score.Max();
topScoreIndex = score.Select((项目,INDX)=>新建项目{=项指数= INDX}),其中(X => x.Item == MAXVAL)。选择(X => x.Index) 。单();


解决方案

我建议编写自己的扩展方法(编辑是通用与 IComparable的< T> 约束。 )

 公共静态INT MaxIndex< T>(这个IEnumerable的< T>序列)
    其中T:IComparable的< T>
{
    INT maxIndex = -1;
    ŧ包括maxValue =默认(T); //立即反正覆盖    INT索引= 0;
    的foreach(序列的T值)
    {
        如果(value.CompareTo(maxValue分别)大于0 || maxIndex == -1)
        {
             maxIndex =指数;
             包括maxValue =价值;
        }
        指数++;
    }
    返回maxIndex;
}

请注意,这个返回-1如果序列是空的。

在特征的字:


  • 这适用于这只能一次列举的序列 - 这个有时可能是非常重要的,并且通常是合乎需要的特征国际海事组织

  • 的存储复杂度为O(1)(而不是为O(n)进行排序)

  • 运行时复杂度为O(n)的(而不是为O(n log n)的分选)

至于是否这LINQ与否:如果它已作为标准LINQ查询运营商之一,你会指望它作为LINQ?是否感觉特别陌生或与其他运营商的LINQ?如果MS是将其包括在.NET 4.0中作为一个新的运营商,这将是LINQ?

编辑:如果你真的,使用LINQ(而不只是获得一个完美的解决方案),真正死心塌地那么这里有一个仍然是O(n),只有评估顺序一次:

  INT maxIndex = -1;
INT索引= 0;
双maxValue(最大值)= 0;INT urgh = sequence.Select(价值=> {
    如果(maxIndex == -1 ||值>包括maxValue)
    {
        maxIndex =指数;
        包括maxValue =价值;
    }
    指数++;
    返回maxIndex;
 })。持续();

这是可怕的,我不建议你使用它在所有 - 但它会奏效

I have an array of doubles and I want the index of the highest value. These are the solutions that I've come up with so far but I think that there must be a more elegant solution. Ideas?

double[] score = new double[] { 12.2, 13.3, 5, 17.2, 2.2, 4.5 };
int topScoreIndex = score.Select((item, indx) => new {Item = item, Index = indx}).OrderByDescending(x => x.Item).Select(x => x.Index).First();

topScoreIndex = score.Select((item, indx) => new {Item = item, Index = indx}).OrderBy(x => x.Item).Select(x => x.Index).Last();

double maxVal = score.Max();
topScoreIndex = score.Select((item, indx) => new {Item = item, Index = indx}).Where(x => x.Item == maxVal).Select(x => x.Index).Single();

解决方案

I suggest writing your own extension method (edited to be generic with an IComparable<T> constraint.)

public static int MaxIndex<T>(this IEnumerable<T> sequence)
    where T : IComparable<T>
{
    int maxIndex = -1;
    T maxValue = default(T); // Immediately overwritten anyway

    int index = 0;
    foreach (T value in sequence)
    {
        if (value.CompareTo(maxValue) > 0 || maxIndex == -1)
        {
             maxIndex = index;
             maxValue = value;
        }
        index++;
    }
    return maxIndex;
}

Note that this returns -1 if the sequence is empty.

A word on the characteristics:

  • This works with a sequence which can only be enumerated once - this can sometimes be very important, and is generally a desirable feature IMO.
  • The memory complexity is O(1) (as opposed to O(n) for sorting)
  • The runtime complexity is O(n) (as opposed to O(n log n) for sorting)

As for whether this "is LINQ" or not: if it had been included as one of the standard LINQ query operators, would you count it as LINQ? Does it feel particularly alien or unlike other LINQ operators? If MS were to include it in .NET 4.0 as a new operator, would it be LINQ?

EDIT: If you're really, really hell-bent on using LINQ (rather than just getting an elegant solution) then here's one which is still O(n) and only evaluates the sequence once:

int maxIndex = -1;
int index=0;
double maxValue = 0;

int urgh = sequence.Select(value => {
    if (maxIndex == -1 || value > maxValue)
    {
        maxIndex = index;
        maxValue = value;
    }
    index++;
    return maxIndex;
 }).Last();

It's hideous, and I don't suggest you use it at all - but it will work.

这篇关于我如何在使用LINQ数组的最高值的指数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆