15,937,012 members
See more:
How can I can get the index of an array element? Is there a method available?

What I have tried:

Nothing.
Posted
Updated 27-Feb-22 2:41am
v2

## Solution 1

There is the Array.IndexOf[^] method, but it may not return the one you want: it will return the first matching entry. So if you have two elements with the value "9", it will always return the one with the lowest index.

Espen Harlinn 14-Feb-11 4:55am
Nice and easy, a 5
Sergey Alexandrovich Kryukov 14-Feb-11 18:00pm
Correct, my 5.
--SA

## Solution 2

Hi Mehdi,

Simply you make a loop for searching for that element in your array.

This is an example:

```int[] Array = { 5, 8, 3, 7 };
int Element = 3;
int Index;

for (int i = 0; i < Array.Length; i++)
{
if (Element == Array[i])
{
Index = i;
}
}```

I hope this help,
:)

## Solution 3

Hope Array Index of[^] will guide you.

## Solution 4

Answer 1 wil give u the first index of the element in array and answer 2 wil give u the last index

You can use List to keep record of all indexes of element to search

C#
```List<int> index_list =new List<int>();
int srch=3;

for (int i = 0; i < Array.Length; i++)
{
if (srch == Array[i])
{
}
}

foreach (int index in index_list) // Loop through List with foreach
{
// do ur coding for index here
}

</int></int>```

if dont want to use List, then can use string too, see here

Unknown size Numeric Arrays from Strings - dontumindit[^]

v3

## Solution 5

lets say:
```public int GetIndexOfArray(string Element, string[] Array)
{
for ( int i = 0; i < Array.Length; i++)
{
if( Element == Array[i])
{
return i;
}
}
return -1;
}

int x = GetIndexOfArray(Element, Array);
/*if x = -1 then there is no such Element, else it will return the index of the Element in the Array*/```

v2