tags:

views:

2026

answers:

7

I have a List with numbers, I'd like to find the position of the minimum (not value) using LINQ

eg: {3,1,0,5} output = 2

+12  A: 
var list = new List<int> { 3, 1, 0, 5 };
int pos = list.IndexOf(list.Min()); // returns 2
John Rasch
What happens if there's another item in the list that's also 0? I'm not criticizing, I just want to know.
mgroves
do list generic objects keep track of the min value in them?
maxwellb
@mgroves - It returns the first index of the minimum
John Rasch
I think IndexOf returns the first index of the specified parameter, or -1 if not found.
maxwellb
(IndexOf) Searches for the specified object and returns the zero-based index of the first occurrence within the entire List<(Of <(T>)>). http://msdn.microsoft.com/en-us/library/e4w08k17.aspx
Greg
A: 
List<int> data = new List<int>();
data.AddRange(new[] { 3, 1, 0, 5 });
Console.WriteLine(data.IndexOf(data.Min()));
Fredrik Mörk
+2  A: 
var data = new List<int> { 3, 1, 0, 5 };

var result = Enumerable.Range(0, data.Count).OrderBy(n => data[n]).First();
dtb
Why is there no `MinBy`... the `Min` overloads are useless...
dtb
A: 
List<int>.Enumerator e = l.GetEnumerator();
int p = 0, min = int.MaxValue, pos = -1;
while (e.MoveNext())
{
    if (e.Current < min)
    {
        min = e.Current;
        pos = p;
    }
    ++p;
}
Razvi
+8  A: 

As you specifically asked for a LINQ solution, and all you got was non-LINQ solutions, here's a LINQ solution:

List<int> values = new List<int> { 3, 1, 0, 5 };

int index =
   values
   .Select((n, i) => new { Value = n, Index = i })
   .OrderBy(n=>n.Value)
   .First()
   .Index;

That however doesn't mean that LINQ is the best solution for this problem...

Guffa
Technically someone else added the LINQ requirement
John Rasch
+1 from me as well... this is the best part about this site - you see 5 different ways to accomplish the exact same thing. "Better" is *usually* a subjective term anyway
John Rasch
Actually, it appears the original poster tagged it LINQ, but didn't mention LINQ in the post.
Don Kirkby
A: 
int min = 0;
bool minIsSet = false;

var result = ints
  .Select( (x, i) => new {x, i}
  .OrderBy(z => z.x)
  .Select(z => 
  {
    if (!minIsSet)
    {
      min = z.x;
      minIsSet = true;
    }
    return z;
  }
  .TakeWhile(z => z.x == min)
  .Select(z => z.i);
David B
A: 

I don't necessarily recommend this CPS-style code, but it works and is O(n), unlike the solutions that use OrderBy:

var minIndex = list.Aggregate(
    new { i = 0, mini = -1, minv = int.MaxValue },
    (min, x) => (min.minv > x)
        ? new { i = min.i + 1, mini = min.i, minv = x }
        : new { i = min.i + 1, mini = min.mini, minv = min.minv })
    .mini;

Change > to >= if you want the last minimum duplicate, not the first.

Use .minv to get the minimum value or neither to get a 2-tuple with both the index and the minimum value.

I can't wait for .NET to get tuples in 4.0.

Joe Chung