H.K
H.K

Reputation: 138

How to get index of repeating elements in an array..?

I am trying to find index of elements stored in an array, some elements are coming repeatedly, when I am trying to get index of those elements its giving the index of first element always. For Eg:

int[] arr = {3,5,6,7,2,3,11,14 };
        int index = Array.IndexOf(arr, 3);
        Console.WriteLine(index);
        Console.ReadLine();

when I want to get the index of 3 as 5, its still giving 0. I cannot skip elements in my logic, I have to check each element every time in my program. PLease help if some can.

Regards.

Upvotes: 0

Views: 3656

Answers (6)

nima
nima

Reputation: 6733

You can have a class that implements IEnumerable and returns the indices you want:

public class Traverse<T> : IEnumerable<int>
{
    T[] _list;
    T _value;

    public Traverse(T[] list, T value)
    {
        this._list = list;
        this._value = value; 
    }

    public IEnumerator<int> GetEnumerator()
    {
        for (int i = 0; i < _list.Length; i++)
            if (_list[i].Equals(_value))
                yield return i;
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

And use it like this:

    int[] arr = { 3, 5, 6, 7, 2, 3, 11, 14 };

    foreach (var index in new Traverse<int>(arr, 3))
        Console.WriteLine(index.ToString());

output: 0 5

Upvotes: 0

LukeHennerley
LukeHennerley

Reputation: 6444

You could use LINQ and the Select(IEnumerable<TSource>, Func<TSource, Int32, TResult>) - here is the MSDN Link.

var indexes = arr.Select((i, index) => new { Number = i, Index = index }).Where(x => x.Number == 3).Select(x => x.Index).ToArray();

Then to get the last index (if that is what you want) use LastOrDefault(), as apose to ToArrray.

Upvotes: 0

Bob Vale
Bob Vale

Reputation: 18474

You want to use Select plus a filter.

public int[] IndexesOf<T>(T[] Values, T find) {
    return Values.Select((i,index) => new { index = index, value = i})
                 .Where(x => x.value == find)
                 .Select(x => x.index)
                 .ToArray();
}

or even as an extension method

public static class MyExtensions {
    public static int[] IndexesOf<T>(this T[] Values, T find) {
        return Values.Select((i,index) => new { index = index, value = i})
                     .Where(x => x.value == find)
                     .Select(x => x.index)
                     .ToArray();
    }
}

Then you can do

var indexes = arr.IndexesOf(3);

Upvotes: 0

Freelancer
Freelancer

Reputation: 9074

Try Below:

int[] arrRepaetInd = new int[arr.Length];
int j=0,cnt=0;
for(int i=0;i<arr.Length;i++)
{
  j++;
  if(arr[i]==arr[i+1]
  {
    cnt++;
    arrRepaetInd[cnt]=arr[i];
    Console.WriteLine(arrRepaetInd[cnt]);
  }
}

arrRepaetInd array is having indexes of repeated elements.

Upvotes: 0

Steve
Steve

Reputation: 216313

I assume that you are searching for a solution indipendent by the number of time the searched term is present in the array. In this case you need a loop where you perform your work on the current item found

int[] arr = {3,5,6,7,2,3,11,14 };
int index = -1;
while((index = Array.IndexOf(arr, 3, index + 1)) != -1)
{
    Console.WriteLine(index);
}

The Array.IndexOf(array, object, startindex) overload will work exactly as you expected

Upvotes: 1

Guffa
Guffa

Reputation: 700592

There is an overload of the IndexOf array that takes a starting index. Use the index of the first item to find the next:

int[] arr = {3,5,6,7,2,3,11,14 };

int index = Array.IndexOf(arr, 3);
Console.WriteLine(index);

int index2 = Array.IndexOf(arr, 3, index + 1);
Console.WriteLine(index2);

Console.ReadLine();

Upvotes: 1

Related Questions