). They contain the same elements, but list 1 contains elements that ...">

Find "not the same" elements in two arrays

I have two whole lists ( List<int> ). They contain the same elements, but list 1 contains elements that are not in list 2.

How to find which elements of list 1 DO NOT appear in list 2.

Thanks:)

PS. lang - c #

+7
arrays list c #
source share
6 answers
 new HashSet<int>(l1).ExceptWith(l2); 
+3
source share

You can use IEnumerable.Except :

 list1.Except(list2); 
+18
source share

A very simple solution:

 HashSet<int> theSet1 = new HashSet<int>(List1); theSet1.ExceptWith(List2); 
+1
source share

For simplicity, you can use the Contains method and check one list that does not contain an element of another:

 for (int i = 0; i < list2.Count; ++i) { if (!list1.Contains(list2[i]) //current element is not in list 1 //some code } 
0
source share

If your solution is that the first list contains the second and you want the onli entries added after the first list, Maybe this will be useful

 public static int DokleSuIsti(IList<string> prevzemNow, IList<string> prevzemOld) { int dobroja = 0; int kolikohinaje; if (prevzemOld.Count() < prevzemNow.Count()) { kolikohinaje = prevzemOld.Count(); } else { kolikohinaje = prevzemNow.Count(); } for (int i = 0; i < kolikohinaje; i++) { if (!Object.Equals(prevzemNow[i], prevzemOld[i])) { dobroja = i; return dobroja; } dobroja = i; } return dobroja; } 

After that, you can use this int as a starting point for going through your Ilist

0
source share

If they are not sorted or something else, it will be difficult for you.

Any O (N ^ 2) algorithm (simple, stupid loop) or additional data structures tell me what you prefer.

Or you can, of course, change the source data by sorting, which I suppose is not an option.

-one
source share

All Articles