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