Solved

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

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

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:…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

776 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