312

How can I find the index of an item in a list without looping through it?

Currently this doesn't look very nice - searching through the list for the same item twice, just to get the index:

var oProp = something;

int theThingIActuallyAmInterestedIn = myList.IndexOf(myList.Single(i => i.Prop == oProp));
1

9 Answers 9

605

How about the List.FindIndex Method:

int index = myList.FindIndex(a => a.Prop == oProp);

This method performs a linear search; therefore, this method is an O(n) operation, where n is Count.

If the item is not found, it will return -1

6
  • 2
    How about int index? Commented Jan 26, 2016 at 0:23
  • 2
    @DylanChensky he's been coding JS too much
    – lenny
    Commented Aug 11, 2016 at 9:40
  • 14
    For reference, if the item is not found; it will return -1 Commented Nov 23, 2018 at 9:44
  • 8
    @lennyy in case you didn't know: "var" is perfectly fine in c#...
    – ims1234
    Commented Sep 4, 2020 at 16:02
  • 2
    @ims1234 I prefer explicit typing for readability. Unless when newing up, no need for the class name twice in one line
    – lenny
    Commented Oct 15, 2020 at 13:23
144

For simple types you can use "IndexOf":

List<string> arr = new List<string>();
arr.Add("aaa");
arr.Add("bbb");
arr.Add("ccc");
int i = arr.IndexOf("bbb"); // Returns 1.
3
  • How do you define "simple types"? Scalar types? Commented Jun 11, 2021 at 20:35
  • @PeterMortensen, yeah! ☺ Commented Jun 13, 2021 at 14:56
  • 1
    This also will return -1 if the element is not found. As Jose already mentioned, if you are using say List<string> this is the way to go, however, it is not feasible for list of complex objects.
    – Ruslan
    Commented Feb 27, 2022 at 21:07
94

EDIT: If you're only using a List<> and you only need the index, then List.FindIndex is indeed the best approach. I'll leave this answer here for those who need anything different (e.g. on top of any IEnumerable<>).

Use the overload of Select which takes an index in the predicate, so you transform your list into an (index, value) pair:

var pair = myList.Select((Value, Index) => new { Value, Index })
                 .Single(p => p.Value.Prop == oProp);

Then:

Console.WriteLine("Index:{0}; Value: {1}", pair.Index, pair.Value);

Or if you only want the index and you're using this in multiple places, you could easily write your own extension method which was like Where, but instead of returning the original items, it returned the indexes of those items which matched the predicate.

5
  • 2
    It seems like all he wants is the index. List<>.FindIndex(Predicate<>) is the best approach. Though the question title would insinuate otherwise, the OP's description is pretty clear he only needs the index "int theThingIActuallyAmInterestedIn" Commented Aug 1, 2013 at 13:52
  • 1
    @LastCoder: Aha - had missed FindIndex. Yes, I completely agree.
    – Jon Skeet
    Commented Aug 1, 2013 at 14:00
  • 1
    Just to be clear, is the "index/value -> single" approach "better" (here meaning being faster in terms of Big-O) than manually iterating twice? Or is the LINQ2Objects provider smart enough to optimize away one of the iterations? (I'm making the assumption that both Select and Single generally speaking are O(n) operations)
    – sara
    Commented Mar 3, 2016 at 7:24
  • 2
    @kai: I think you need to read up on how LINQ works, basically. It's too complicated to explain in detail in a comment. However... this is only iterating over the source collection once. LINQ sets up a pipeline, which lazily transforms the input sequence into another sequence, and then the Single() operation iterates over that sequence and finds the single item which matches the predicate. For more details, read my edulinq blog series: codeblog.jonskeet.uk/category/edulinq
    – Jon Skeet
    Commented Mar 3, 2016 at 7:38
  • 1
    +1 I needed this solution. Boss thought I was smart for once. I was advised to carefully document this, since it used an anonymous type and may not be clear to the next coder in the area.
    – Adam Wells
    Commented Jul 23, 2018 at 17:33
17

If you don't want to use LINQ, then:

int index;
for (int i = 0; i < myList.Count; i++)
{
    if (myList[i].Prop == oProp)
    {
       index = i;
       break;
    }
}

This way you are iterating the list only once.

3
  • 22
    @KingKing noone said it is.
    – Tomer W
    Commented Aug 1, 2013 at 14:01
  • 1
    Is this the same implementation as Linq FindIndex out of interest?
    – Paul C
    Commented Feb 2, 2016 at 12:31
  • 2
    probably not the same code, List has some neat optimizations here and there. but I find it hard to believe they can search an unordered list in less than O(n), so I'd say they're probably really similar in practice.
    – sara
    Commented Mar 3, 2016 at 7:25
16
  1. A simple solution to find the index for any string value in the List.

    Here is code for a list of strings:

     int indexOfValue = myList.FindIndex(a => a.Contains("insert value from list"));
    
  2. A simple solution to find the index for any integer value in the List.

    Here is code for a list of integers:

     int indexOfNumber = myList.IndexOf(/* insert number from list */);
    
6

If anyone wonders for the Array version, it goes like this:

int i = Array.FindIndex(yourArray, x => x == itemYouWant);
3

Here's a copy/paste-able extension method for IEnumerable

public static class EnumerableExtensions
{
    /// <summary>
    /// Searches for an element that matches the conditions defined by the specified predicate,
    /// and returns the zero-based index of the first occurrence within the entire <see cref="IEnumerable{T}"/>.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="list">The list.</param>
    /// <param name="predicate">The predicate.</param>
    /// <returns>
    /// The zero-based index of the first occurrence of an element that matches the conditions defined by <paramref name="predicate"/>, if found; otherwise it'll throw.
    /// </returns>
    public static int FindIndex<T>(this IEnumerable<T> list, Func<T, bool> predicate)
    {
        var idx = list.Select((value, index) => new {value, index}).Where(x => predicate(x.value)).Select(x => x.index).First();
        return idx;
    }
}

Enjoy.

1

And IndexOf can be used directly with the object without an explicit comparison.

List<YourObject> listObj = new List<YourObject>();
    var obj = new YourObject();
    int idx = listObj.IndexOf(obj);
1
  • Thank you for your interest in contributing to the Stack Overflow community. This question already has quite a few answers—including one that has been extensively validated by the community. Are you certain your approach hasn’t been given previously? If so, it would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient. Can you kindly edit your answer to offer an explanation? Commented Jul 13, 2023 at 3:36
0

That's all fine and good -- but what if you want to select an existing element as the default? In my issue there is no "--select a value--" option.

Here's my code -- you could make it into a one liner if you didn't want to check for no results I suppose...

private void LoadCombo(ComboBox cb, string itemType, string defVal = "")
{
    cb.DisplayMember = "Name";
    cb.ValueMember = "ItemCode";
    cb.DataSource = db.Items.Where(q => q.ItemTypeId == itemType).ToList();

    if (!string.IsNullOrEmpty(defVal))
    {
        var i = ((List<GCC_Pricing.Models.Item>)cb.DataSource).FindIndex(q => q.ItemCode == defVal);
        if (i>=0) cb.SelectedIndex = i;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.