ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

NoDuplicates

Method to remove duplicate entries from either a string or integer array

Source

<System.Runtime.CompilerServices.Extension()> _
Public Function NoDuplicates(ByVal values As String()) As String()
    Return (From value In values Select value Distinct Order By value).ToArray
End Function
<System.Runtime.CompilerServices.Extension()> _
Public Function NoDuplicates(ByVal values As Integer()) As Integer()
    Return (From value In values Select value Distinct Order By value).ToArray
End Function

Example

Dim Names As String() = New String() {"Bob", "Mary", "Jane", "Bob", "Joe", "Bill"}
Dim SingleNames As String() = Names.NoDuplicates

Array.ForEach(Of String)(Names, AddressOf Console.WriteLine)
Console.WriteLine()
Array.ForEach(Of String)(SingleNames, AddressOf Console.WriteLine)

Console.WriteLine("-----------------------")

Dim Numbers As Integer() = New Integer() {1, 2, 3, 4, 5, 5, 6, 7, 8, 8, 9}
Dim SingleNumbers As Integer() = Numbers.NoDuplicates
Array.ForEach(Of Integer)(Numbers, AddressOf Console.WriteLine)
Console.WriteLine()
Array.ForEach(Of Integer)(SingleNumbers, AddressOf Console.WriteLine)

Author: Kevin S Gallagher

Submitted on: 26 okt. 2009

Language: VB

Type: System.Array

Views: 4614