Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VB

Remove duplicate entries from string array

3.00/5 (5 votes)
4 Mar 2010CPOL 1  
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
VB
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
VB
bob
Bob
Jane
Kevin
Mary


Extension (place in code module)
XML
<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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)