2

I'm hoping this will be fairly simple and down to my lack of knowledge as a beginner but I'm trying to see if an array of ints with two elements is in a List.

Int[] meh = {1,2};

List<int[]> list1 = new List<int[]>();

List1.Add(meh);

Int[] meh2 = {1,2};

If(List1.Contains(meh2))
{
    Console.WriteLine(“Found it”);
}

From reading around I gather that the array wont be found as it's to do with how Lists compare objects by reference and not value ... all examples Ive found have been to find a single int within an array in a List but not the array as a whole.

Im vaguely aware that List.Find() may be useful here but again I cant see how to use LINQ for matching both elements in each array in the list.

Any help or pointers to reading material greatly appreciated.

Thanks in advance.

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
Dan
  • 127
  • 2
  • 2
  • 9
  • The comparison is made by reference. You should code your own `Contains` method. – dcg Jun 09 '17 at 12:47

3 Answers3

3

How about this

if(list1.Any(x => x.SequenceEqual(meh2)))
{
   Console.WriteLine("Found it");
}
Samuil Petrov
  • 542
  • 1
  • 13
  • 24
  • 1
    Beautiful! Thankyou. Short, works and easy to understand. I'll mark this as the answer when it let's me! – Dan Jun 09 '17 at 12:57
  • As an extension to this - now i know it's in the List, is there something as equally simple to remove it? as the List.remove() will have the same issue for finding by comparing the object and returning an index location? – Dan Jun 09 '17 at 13:00
  • Hi again, I think something like this should do the trick: list1.RemoveAll(x => meh2.Contains(x)); (haven't tested it). Considering you invoke it with checking the previous thing, otherwise it may partially remove elements that you don't want to. Edit: I think this may not be secure cause it will still remove elements containing in the array but positioned in the list at different indexes. – Samuil Petrov Jun 09 '17 at 13:06
1

You can use Enumerable.SequenceEqual

that will return -1 if the sequence is not found

example:

var myArr = new int[] { 3, 3 };
List<int[]> ListOfArrays = new List<int[]>
{
    new int[] { 0, 0 },
    new int[] { 2, 2 },
    new int[] { 1, 1 },
    new int[] { 3, 3 }
};
var index = ListOfArrays.FindIndex(l => Enumerable.SequenceEqual(myArr, l));
Console.WriteLine("here: " + index);
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

Not sure what you want to achieve exactly. If list1 has an element [1, 2, 3] and you search for [1, 2], would that be a match, or do all elements have to match? Do they have to be in the same order? ...

Whatever you want to achieve exactly, you can do it with Any() and SequenceEqual()

int[] meh = {1, 2};
int[] meh2 = {1, 5};
var list1 = new List<int[]>() {meh, meh2};

if (list1.Any(x => x.SequenceEqual(new[] {1, 5})))
{
    Console.WriteLine("Found it");
}

Also see this answer. It contains an example where both arrays are sorted first (ie ignoring the order of the elements in the array)

Laoujin
  • 9,962
  • 7
  • 42
  • 69