Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare two hashsets?

Tags:

c#

I have two hashsets like this:

HashSet<string> log1 = new HashSet<string>(File.ReadLines("log1.txt"));
HashSet<string> log2 = searcher(term);

How would I compare the two?

I want to make sure that log2 does not contain any entries from log1. In other words, I want to remove all(if any), items that log1 has inside log2.

like image 669
user1213488 Avatar asked May 17 '12 12:05

user1213488


1 Answers

To remove all items from log2 that are in log1, you can use the HashSet<T>.ExceptWith Method:

log2.ExceptWith(log1);

Alternatively, you can create a new HashSet<T> without modifying the two original sets using the Enumerable.Except Extension Method:

HashSet<string> log3 = new HashSet<string>(log2.Except(log1));
like image 90
dtb Avatar answered Oct 14 '22 06:10

dtb