Depude and randomise 2 lists

I was asked to take 2 lists of names and dedupe the newer list with the names in the older list, so we only had a list of new names in a specific period (no dates here to help out!).  Turned out to be a lot simpler than I thought.  At first I thought Excel spreadsheet as the data wasn’t in a database.  I thought I could do some fancy filter action, but nope!  That was harder than a diamond on a bouncer’s knuckle!

Solution

The solution was a real simple tiny bit of .NET code:

 var likes = System.IO.File.ReadAllLines(@"D:\likes.txt");
 var olds = System.IO.File.ReadAllLines(@"D:\lomb-likes.txt");
 var duped = new List<string>();

 Array.ForEach(likes, like =>
 {
     var found = false;
     Array.ForEach(olds, old =>
     {
         if(old == like)
         {
             found = true;
         }
     });

     if(!found)
     {
         duped.Add(like);
     }
 });

 var result = duped.OrderBy(dupe => Guid.NewGuid()).ToList();

Simples!  Now get on with your work and stop reading my blog!

3 thoughts on “Depude and randomise 2 lists”

  1. Could also have used the built in Enumerable.Except method to save some coding:

    var duped = likes.Except(olds);

  2. Bugger n botherations! That’s about as simple as it gets!! It’s the wee things you find out about that make all the difference…and those are the same wee things that no one blogs about and you need to go on training courses or spend time at home playing with each option!

    Nice one Neil, thanks for that!

  3. No problem. I know exactly what you mean, as I always find out about stuff right this right after it would have been useful. 🙂

Leave a Reply

Your email address will not be published.