?
Solved

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

Posted on 2014-10-02
3
Medium Priority
?
262 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 35

Accepted Solution

by:
ste5an earned 2000 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 34

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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

752 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