65.9K
CodeProject is changing. Read more.
Home

Remove duplicate entries from string array

starIconstarIconstarIconemptyStarIconemptyStarIcon

3.00/5 (5 votes)

Mar 4, 2010

CPOL
viewsIcon

37373

This VS2008 or higher extension uses a LINQ statement to remove duplicate items from a string array but is not case sensitive, also you might like to leave the sort/order-by in the extension or remove it.ExampleDim Names() As String = {"Bob", "Mary", "bob", "Bob", "Jane", "Kevin",...

This VS2008 or higher extension uses a LINQ statement to remove duplicate items from a string array but is not case sensitive, also you might like to leave the sort/order-by in the extension or remove it. Example
Dim Names() As String = {"Bob", "Mary", "bob", "Bob", "Jane", "Kevin", "Jane"}
Names = Names.RemoveDuplicates
For Each Name As String In Names
    Console.WriteLine(Name)
Next
Returns
bob
Bob
Jane
Kevin
Mary
Extension (place in code module)
<System.Diagnostics.DebuggerStepThrough()> _
<System.Runtime.CompilerServices.Extension()> _
Public Function RemoveDuplicates(ByVal sender As String()) As String()
    Return (From value In sender Select value Distinct Order By value).ToArray
End Function