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.


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..


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!




Comments (5)
  1. Sam says:

    Ok, I understand why it removes the empty items.

    But why are the duplicates removed? This I would have expected!

  2. Dominic Wate says:


    In response to Sam, check this out:…/Enumerable-Except-TSource-and-IEqualityComparer-a-little-help.aspx

    Specifically this line:

    "The Except operator returns the set difference of two sequences by using the default equality comparer to compare values. In proper English this means that the Except method returns those elements in first sequence that do not appear in second by simply comparing the Objects themselves."

    Also here:…/bb300779(v=vs.100).aspx

    Look at the first comment.

    And lastly, comment from @Saeed here:…/remove-duplicate-item-from-list-based-on-condition

    Still not sure what the mathematical basis is for excluding differences, but it definitely does 🙂

  3. teoman says:

    I do not see where you are removing duplicates?

  4. Dominic Wate says:

    Hi teoman,

    Check out the links in my response to Sam (at 07-11-2014 3:12 PM) – Except does this.

  5. vamittal says:

    Thanks Dominic for sharing the articles:

    @Sam, teoman: This is how internally the Except function works.

    The msdn article (…/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.


Comments are closed.

Skip to main content