0

I am trying to find different combinations of values that will solve a solution. If the values (in a double array) pass a test I want to add them to a list, providing they are not already in the list.

If the list contains an array with values [1, 2, 3, 4, 5] and I check to see if the list contains array [5, 4, 3, 2, 1] List.Contains returns true. Is there anyway to search a list of arrays where order of the array matters?

I have tried List.Any(array.SequencyEqual) but that seems to have the same issue.

if(!myList.Any( a => a.SequenceEqual(myArray)))
{
    //some code to print array values 
    myList.Add(myArray);
}

This if statement executes true once, then never again.

6
  • If the order matters, you are looking for permutations, not combinations. Commented May 21, 2015 at 13:57
  • @Jodrell This doesn't seem to be about combinatorics. Commented May 21, 2015 at 13:57
  • 2
    SequenceEqual, as the name suggests, does check the specific ordering. Show us the code you used that isn't working and explain what your expected outcome is. Commented May 21, 2015 at 13:58
  • 3
    Wasn't able to reproduce with the values provided. Please post some code. Commented May 21, 2015 at 13:58
  • So you have a List<double[]>? Commented May 21, 2015 at 14:00

2 Answers 2

4

I fear you are mistaken, run this simple test program

using System;
using System.Collections.Generic;
using System.Linq;

public class Test
{
    public static void Main()
    {
        var list = new List<double[]>
            {
                new[] { 1.0, 2.0, 3.0, 4.0, 5.0 }
            };
        Console.WriteLine(
            list.Any(a => a.SequenceEqual(new[] { 5.0, 4.0, 3.0, 2.0, 1.0 })));
        Console.WriteLine(
            list.Any(a => a.SequenceEqual(new[] { 1.0, 2.0, 3.0, 4.0, 5.0 })));
    }
}

this program outputs

False
True

as I would expect.

Sign up to request clarification or add additional context in comments.

3 Comments

@Mike159 show all code in your question required to reproduce your problem. Until you do that, any help offered is based on guesswork and assumptions.
@Mike159 I'm drawn to agree. However, its not obvious exactly what is wrong until we can see it in front of us.
Can't get it to format nicely. I have added my code for the SequenceEqual part in my orignal question.
0

There's probably a fancy way to do this in LINQ, but here's the old fashioned way...

List<int[]> list = new List<int[]>();
int[] array1 = new int[]{ 1, 2, 3, 4, 5 };
int[] array2 = new int[]{ 5, 4, 3, 2, 1 };
list.Add(array1);
// add other arrays to list
bool found = false;
foreach (int[] other in list)
{
    bool isMatch = true;
    if (array2.Length == other.Length)
    {
        for (int i = 0; i < array2.Length; i++)
        {
            if (array2[i] != other[i])
            {
                isMatch = false;
                break;
            }
        }
    }
    else
    {
        isMatch = false;
    }

    if (isMatch)
    {
        found = true;
        break;
    }
}

Now, use found to decide what to do with your array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.