Solved

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

Posted on 2014-10-02
3
255 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 32

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 74

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 32

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 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

10 Experts available now in Live!

Get 1:1 Help Now