Lost in Technology

My contribution to SharePoint development/Configuration

Removing duplicate and empty items from an Array using Linq

Removing duplicate and empty items from an Array using Linq

Rate This
  • Comments 5

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

 

 

Leave a Comment
  • Please add 7 and 8 and type the answer here:
  • Post
  • Ok, I understand why it removes the empty items.

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

  • Hi,

    In response to Sam, check this out: blog.dreamlabsolutions.com/.../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:

    msdn.microsoft.com/.../bb300779(v=vs.100).aspx

    Look at the first comment.

    And lastly, comment from @Saeed here:

    stackoverflow.com/.../remove-duplicate-item-from-list-based-on-condition

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

  • I do not see where you are removing duplicates?

  • Hi teoman,

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

  • 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

Page 1 of 1 (5 items)