-2

I have a list of colors as shown in the expanded code screenshot below. The list will always have 4 color values, but only one of them will be different than other 3.

But this one different value can sit at any place in the list.

What I need is, get only the index of that item which is different than other 3.

I have tried playing with distinct but it doesn't help since I only need the index not the value.

enter image description here

Usman Waheed
  • 70
  • 1
  • 8
  • 2
    After you find the distinct value, you can .indexOf() https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.indexof?view=net-6.0 – Jasen May 06 '22 at 19:02
  • as per the above screenshot, it will return 2 distinct values (Yellow and Window), so how do I know which one of them is unique in the original 4 elements list ? – Usman Waheed May 06 '22 at 19:13
  • 1
    Iterate through the list and count the values. Then find the value that has only one occurrence. – Pranav Hosangadi May 06 '22 at 19:19
  • @PranavHosangadi thx for this suggestion, I thought that would be dirty trick. Is there any better generic solution ? – Usman Waheed May 06 '22 at 19:21
  • 1
    You can also _group by_ and _count_ https://stackoverflow.com/questions/687313/building-a-dictionary-of-counts-of-items-in-a-list. But do you really need the index? Sounds like an XY problem. – Jasen May 06 '22 at 19:21
  • Can you define what you mean by "better generic solution"? – Pranav Hosangadi May 06 '22 at 19:22
  • @Jasen that sounds workable, will try it, thx – Usman Waheed May 06 '22 at 19:42
  • @PranavHosangadi just my gutt feeling, as I often see people doing things with LINQ or default list methods, so I thought a possible such solution would be better. – Usman Waheed May 06 '22 at 19:43

0 Answers0