Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello.
I am sure, that this question has been already answered, but I just can't find the question with my low expression abilities. So, let's say I have a list named list1:
a, b, c

I want to chech if another list (list2) doesn't contain other elements than these in list1.
So if list2 is:
a, b, c, d

It will return false, but also if some elements of list1 are missing, it will still return true. Hopefully you understood me and sorry for my bad english, still learning. Thanks for reading!
Posted
Comments
ZurdoDev 8-Jan-15 8:44am    
I believe LINQ could help you so you don't have to do nested foreach loops. Where are you stuck?
Samuel Dudík 8-Jan-15 8:49am    
Well, I'm stuck at everything about this. I don't know where to start. I try to search in LINQ as you mentioned, thanks.
ZurdoDev 8-Jan-15 8:50am    
I googled "c# compare list of strings" and quite a few examples are there.
PIEBALDconsult 8-Jan-15 9:17am    
Stick each in a Hashset and use set operations -- is set B a subset of set A. (That's all Linq will do anyway, cut out the middleman.)
BillWoodruff 8-Jan-15 14:17pm    
That's a very interesting idea: it would be useful to see some timing comparisons of using Linq vs. using HashTables.

Using the code below I got the following results:

100000000 00:00:45.9990976
200000000 00:00:21.1352109
300000000 00:00:17.3039397


So, for the given data...
Set.IsSupersetOf is quickest and I recommend it for your needs (whatever they are).
Set.IsSubsetOf is not quite as quick -- I expect it has to iterate more of the List.
And the Linqish List.Except is worst, including the situation that if you just want a boolean then there is no need to produce a collection and then count the items in the collection.

Consider, that when seeking a boolean, the process can be short-circuited as soon as one item is found outside the target. I certainly hope that IsSupersetOf and IsSubsetOf do that. Except certainly does not short-circuit and then it makes you perform another operation (like Count) before you can get your result.

using System.Linq ;
using StringList=System.Collections.Generic.List<string> ;
using StringSet=System.Collections.Generic.HashSet<string> ;

        int reps = 100000000 ;

        StringList list1 = new StringList { "a" , "b" , "c" , "z" } ;
        StringList list2 = new StringList { "a" , "b" , "c" , "d" } ;
        StringSet  set1  = new StringSet  ( list1 ) ;
        StringSet  set2  = new StringSet  ( list2 ) ;

        System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch() ;


        sw.Restart() ;

        for ( int i = 0 ; i < reps ; i++ )
          result += list2.Except(list1).Count()==0 ? 0 : 1 ;

        sw.Stop() ;

        System.Console.WriteLine ( "{0} {1}" , result , sw.Elapsed ) ;


        sw.Restart() ;

        for ( int i = 0 ; i < reps ; i++ )
          result += set2.IsSubsetOf ( list1 ) ? 0 : 1 ;

        sw.Stop() ;

        System.Console.WriteLine ( "{0} {1}" , result , sw.Elapsed ) ;


        sw.Restart() ;

        for ( int i = 0 ; i < reps ; i++ )
          result += set1.IsSupersetOf ( list2 ) ? 0 : 1 ;

        sw.Stop() ;

        System.Console.WriteLine ( "{0} {1}" , result , sw.Elapsed ) ;
 
Share this answer
 
v2
Comments
Maciej Los 8-Jan-15 17:24pm    
+5!
BillWoodruff 8-Jan-15 20:23pm    
+5 Great to see tests like this ! Might it improve test result quality if you ran each test once before timing in order to make sure everything is fully jitted ?
PIEBALDconsult 8-Jan-15 21:38pm    
You have the code. I also wonder whether or not you can devise an even-more Linqish scenario, with a lambda.
Based on tis article: How to: Find the Set Difference Between Two Lists (LINQ)[^], you can use Linq to find differences between two string lists:

C#
List<string> list1 = new List<string>{"a","b","c"};
List<string> list2 = new List<string>{"a","b","c", "d"};

var hasMatch = list2.Except(list1);


Returns: d

[EDIT]
Thank you for all valuable comments. It helps me to improve my answer.

Another interestinf method to compare two string list is: SequenceEqual, which returns boolean value.
C#
bool listsAreEqual = list2.SequenceEqual(list1);


For further information, please see: Enumerable.SequenceEqual<TSource> Method (IEnumerable<TSource>, IEnumerable<TSource>)[^].
 
Share this answer
 
v3
Comments
Praveen Kumar Upadhyay 8-Jan-15 9:29am    
Hey I am one of the top experts in last 24 hours. I am very happy.
Maciej Los 8-Jan-15 9:30am    
I'm happy with you ;) Congrats!
Praveen Kumar Upadhyay 8-Jan-15 9:31am    
Thank you very much. How can I record this moment in code project.
Maciej Los 8-Jan-15 9:32am    
Take a picture or remember it in memory ;)
There's no way to "record" it.
Praveen Kumar Upadhyay 8-Jan-15 9:34am    
Okay. Can't I make a note somewhere in code project itself, So that someone can visit it and can know.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900