Getting index from unqiue (stand-alone) elements in C#

Viewed 102

I'd like to get the index of elements that stand alone. It is possible that the elements themselves appear more often in the list (one after the other or mixed). The indication for a single element is that the predecessor and successor are not equal to the current element. Is there an elegant way to do this?

Example:

1.  A
2.  A
3.  A
4.  B
5.  B
6.  A
7.  B
8.  B
9.  C
10. B

Result:

6,9,10
5 Answers

simple iterate over the items and check for that condition

char[] items = { 'A', 'A', 'A', 'B', 'B', 'A', 'B', 'B', 'C', 'B' };
for (int i = 0; i < items.Length; i++)
{
    if (i == 0)
    {
        // in case of the first element you only have to validate against the next
        if (items[i] != items[i + 1])
            Console.WriteLine(i + 1);
    }
    else if (i == items.Length - 1)
    {
        // in case of the last element you only have to validate against the previous       
        if (items[i] != items[i - 1])
            Console.WriteLine(i + 1);
    }
    else
    {
        // validate against previous and next element
        if (items[i] != items[i - 1] && items[i] != items[i + 1])
            Console.WriteLine(i + 1);
    }
}

https://dotnetfiddle.net/kWmqu7

Here is one solution I came up with:

So you have your example list like that:

var list = new List<string>
{
    "A", // 1
    "A", // 2
    "A", // 3
    "B", // 4
    "B", // 5
    "A", // 6
    "B", // 7
    "B", // 8
    "C", // 9
    "B"  // 10
};

and then you call a method called GetSingleListPositions and receive List<int> representing your desired positions.

private static List<int> GetSingleListPositions(IList<string> list)
{
    var uniquePositions = new List<int>();
    var occurence = new List<string>();

    for (int i = list.Count - 1; i >= 0; i--)
    {
        if (!occurence.Contains(list[i]))
        {
            occurence.Add(list[i]);
            uniquePositions.Add(++i);
        }
    }

    uniquePositions.Reverse();
    return uniquePositions;
}

You call it like this:

var result = GetSingleListPositions(list);
Console.WriteLine(string.Join(' ', result));

As a result, I receive this:

6 9 10

Hope this helps, Cheers

Here's a solution that works in O(N) time even if the input sequence does not support indexing. It also works for any type that implements a proper Equals():

public static IEnumerable<int> IndicesOfStandaloneElements<T>(IEnumerable<T> elements)
{
    int index = 0;

    T previous = default;
    T current  = default;

    var comparer = EqualityComparer<T>.Default;

    foreach (var next in elements)
    {
        if (index == 1)
        {
            if (!comparer.Equals(current, next))
                yield return 0;
        }
        else if (index > 1)
        {
            if (!comparer.Equals(current, previous) && !comparer.Equals(current, next))
                yield return index - 1;
        }

        previous = current;
        current  = next;

        ++index;
    }

    if (index > 0 && !comparer.Equals(current, previous))
        yield return index - 1;
}

Assuming that you want last index of the elements. Try below code.

    string[] abc = new string[]{"A","A","A","B","B","A","B","B","C","B"};

    var distinctValues = abc.Distinct().Select(x => x);
    foreach (var d in distinctValues)
    {
        Console.WriteLine(Array.LastIndexOf(abc.ToArray(), d));
    }

//Result
5
9
8
using System.Linq;
using System;

Add your namespace and class

**variables needed**
string[] printer = {"A", "A", "A", "B", "B","A","B","B","C","B"};
int[] terms = new int[10];
int check=0;
 int index1=0;
**method to check the value present in array and add index if not present in array for your output**

for (int i = i; i <= printer.Length; i++)
{
   int test=findIndex(i,printer.Length,printer[i]);
if(test==0)
{
  addIndex(i);
}
}
**method to addindex delare**

public addIndex(int num)
{
    terms[index1] = value;
    index1=index1+1;

}
**method to findIndex**

public int findIndex(int num1, int num2,string test) {
for (int i = num1; i < num2; i++)
{
    if (string.Compare(printer[i],test)==0 )
        {
            return 1;
       }

}
    return 0;

}
Related