64

Say we have 5 string arrays as such:

string[] a = {"The","Big", "Ant"};
string[] b = {"Big","Ant","Ran"};
string[] c = {"The","Big","Ant"};
string[] d = {"No","Ants","Here"};
string[] e = {"The", "Big", "Ant", "Ran", "Too", "Far"};

Is there a method to compare these strings to each other without looping through them in C# such that only a and c would yield the boolean true? In other words, all elements must be equal and the array must be the same size? Again, without using a loop if possible.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Wes Field
  • 3,291
  • 6
  • 23
  • 26
  • You could simplify the comparison using string.Join and you could get away from loops using Linq but under the hood you are still looping - are you trying to do it without loops for a reason? – Charleh Jun 20 '13 at 11:21
  • look like duplicate as http://stackoverflow.com/questions/2913287/comparing-arrays-using-linq-in-c-sharp and http://stackoverflow.com/questions/713341/comparing-arrays-in-c-sharp – Atish Kumar Dipongkor Jun 20 '13 at 11:49

5 Answers5

111

You can use Linq:

bool areEqual = a.SequenceEqual(b);
Ahmed KRAIEM
  • 10,267
  • 4
  • 30
  • 33
  • 8
    @WesField: Note that this method also loops, just because your requirement was _"without looping"_. Of course it's impossible to compare multiple items without looping. Also note that it uses the default comparer by default which works for value types and the .NET types. For custom reference types you need to create a custom `IEqualityComparer` for [`SequenceEqual`](http://msdn.microsoft.com/en-us/library/bb342073.aspx) and/or override `Equals` and `GetHashCode`. – Tim Schmelter Jun 20 '13 at 11:36
  • 1
    @TimSchmelter: Yeah, I realize that a loop is being executed behind the scenes, I just wanted something neat and pretty-looking without room for a muck up. – Wes Field Jun 20 '13 at 11:39
  • 18
    Returns false when two arrays have exactly same values, but in different order. – tdgtyugdyugdrugdr Aug 20 '14 at 07:30
  • "Without looping" is handy when you need to construct an Expression>. This is what I ended up doing for Mock.Is from the Moq library. – Nelson Apr 12 '17 at 20:06
25

Try using Enumerable.SequenceEqual:

var equal = Enumerable.SequenceEqual(a, b);
Darren
  • 68,902
  • 24
  • 138
  • 144
  • 1
    Also a good answer. Could you explain the differences between this and the Linq a.SequenceEqual(b)? – Wes Field Jun 20 '13 at 11:33
  • 2
    Same thing - ones using the extension method syntax, the other is using the extension method explicitly. Look at extension method sig for details http://msdn.microsoft.com/en-us/library/vstudio/bb383977.aspx – Charleh Jun 20 '13 at 11:37
  • 3
    @WesField: There's no difference. An extension method is just a static method in a static class. So you can treat it as a normal static method(this answer) or you use it as extension method(Ahmed's answer). – Tim Schmelter Jun 20 '13 at 11:43
6

if you want to get array data that differ from another array you can try .Except

string[] array1 = { "aa", "bb", "cc" };
string[] array2 = { "aa" };

string[] DifferArray = array1.Except(array2).ToArray();

Output: {"bb","cc"}

Manish Vadher
  • 1,524
  • 15
  • 14
3

If you want to compare them all in one go:

string[] a = { "The", "Big", "Ant" };
string[] b = { "Big", "Ant", "Ran" };
string[] c = { "The", "Big", "Ant" };
string[] d = { "No", "Ants", "Here" };
string[] e = { "The", "Big", "Ant", "Ran", "Too", "Far" };

// Add the strings to an IEnumerable (just used List<T> here)
var strings = new List<string[]> { a, b, c, d, e };

// Find all string arrays which match the sequence in a list of string arrays
// that doesn't contain the original string array (by ref)
var eq = strings.Where(toCheck => 
                            strings.Where(x => x != toCheck)
                            .Any(y => y.SequenceEqual(toCheck))
                      );

Returns both matches (you could probably expand this to exclude items which already matched I suppose)

Charleh
  • 13,749
  • 3
  • 37
  • 57
-2
        if (a.Length == d.Length)
        {
            var result = a.Except(d).ToArray();
            if (result.Count() == 0)
            {
                Console.WriteLine("OK");
            }
            else
            {
                Console.WriteLine("NO");
            }
        }
        else
        {
            Console.WriteLine("NO");
        }
Dejan Ciev
  • 142
  • 4