Compartilhar via


Removing duplicate and empty items from an Array using Linq

Most of the people in this world might be aware of the solution but sharing it ...just in case if anyone might find it useful.

I was working on a code sample to remove all the duplicate and empty values from a String Array. I was trying a few approaches before I encountered the one solution using Linq that solved my problem.

The best part of it that I liked was that the approach was rather cleaner, easier, simple and elegant of all the ones i tried before.

Problem:

Suppose you have an Array object say testArray of type string. The declaration of the array will be something like:

string[] testArray = new string[] {"A", "B", "C", "D","E","B","A","","E","","A","D","C"};

Now, you want the final result that would only contain following values:

Required result:

"A", "B", "C", "D", "E" - Since they are all unique values and do not contain empty values like "" as well.

So, what would be your approach? Writing custom logic using loops? NAH..

Solution:

I used a rather simple approach using Linq:

testArray = testArray .Except(new string[] { "" }).ToArray();

Final Result:

The final values found in the array were as per the expectations:

"A", "B", "C", "D", "E"

IMO, the unique items are saved using Linq when the Linq tried to run equality comparer to compare values between source and resultant array.

 

Happy Coding!

/Vaibhav

Comments

  • Anonymous
    July 10, 2014
    Ok, I understand why it removes the empty items. But why are the duplicates removed? This I would have expected!

  • Anonymous
    July 10, 2014
    The comment has been removed

  • Anonymous
    July 10, 2014
    I do not see where you are removing duplicates?

  • Anonymous
    July 11, 2014
    Hi teoman, Check out the links in my response to Sam (at 07-11-2014 3:12 PM) - Except does this.

  • Anonymous
    July 11, 2014
    Thanks Dominic for sharing the articles: @Sam, teoman: This is how internally the Except function works. The msdn article (msdn.microsoft.com/.../bb300779(v=vs.100).aspx) shared by Dominic clearly mention the following statement: "This method returns those elements in first that do not appear in second. It does not also return those elements in second that do not appear in first." You could find this in the Remarks section. /Vaibhav