Solved

C# determine if list of strings have same value without regard to position

Posted on 2014-10-02
3
257 Views
Last Modified: 2016-02-17
I have two list of strings I am getting from a function.  How can I tell if they are identical without respect to the order.

meaning

list1 = {"a", "b", "c"}
list2 = {"c", "b", "a"}
list3 = {"a")

list1 and list2 would be equal
list1 and list3 would not be equal
list2 and list3 would not be equal
0
Comment
Question by:jackjohnson44
3 Comments
 
LVL 33

Accepted Solution

by:
ste5an earned 500 total points
ID: 40357344
E.g. using Linq:

namespace Samples
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    public class Sample
    {
        public static void Main()
        {
            List<string> list1 = new List<string>() { "a", "b", "c" };
            List<string> list2 = new List<string>() { "c", "b", "a" };
            List<string> list3 = new List<string>() { "a" };

            if (list1.Except(list2).Count() == 0)
                Console.WriteLine("list1=list2");

            if (list1.Except(list3).Count() == 0)
                Console.WriteLine("list1=list3");

            if (list2.Except(list3).Count() == 0)
                Console.WriteLine("list2=list3");

            Console.ReadLine();
        }
    }  
}

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40357356
If you cannot use LINQ (for whatever reason), then the "old school" approach would be to sort the lists and then compare  each element:

bool areEqual = true;

list1.Sort();
list2.Sort();

if (list1.Length != list2.Length)
{
    areEqual = false;
}
else
{
    for (int i = 0; i < list1.Length; i++)
    {
        if (list1[i] != list2[i])
        {
            areEqual = false;
        }
    }
}

Open in new window

0
 
LVL 33

Expert Comment

by:it_saige
ID: 40357510
Normally I would say that Enumerable.SequenceEqual would suffice, but in this case (since the items are out of order) it would not.  So you would sort the items before comparing:
Enumerable.SequenceEqual(list1.OrderBy(i => i), list2.OrderBy(i => i))

Open in new window


For custom types, you will have to implement a comparer if the standard comparer does not work.  In those cases, code would be changed to:
Enumerable.SequenceEqual(list1.OrderBy(i => i, myListComparer), list2.OrderBy(i => i, myListComparer))

Open in new window


More information on implementing a comparer:

http://msdn.microsoft.com/en-us/library/234b841s(v=vs.110).aspx

-saige-
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
asp.net mvc input file 2 32
Need to pass a connection through class/Method 1 39
Why Does This Page Not Show Up? 4 22
What .NET URL re-routing tool did I use? 2 34
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now