ali raza

C#, .Net ,Asp.net and my thoughts

How to remove duplicate from string array in .net

In one case i have to right a method to remove duplication from array of strings. 
i write this code 

Public Function RemoveDuplicates(ByVal items As String()) As String()
    Dim noDupsArrList As New ArrayList()
    For i As Integer = 0 To items.Length - 1
        If Not noDupsArrList.Contains(items(i).Trim()) Then
            noDupsArrList.Add(items(i).Trim())
        End If
    Next
    
    Dim uniqueItems As String() = New String(noDupsArrList.Count - 1) {}
    noDupsArrList.CopyTo(uniqueItems)
    Return uniqueItems
End Function

7 responses to “How to remove duplicate from string array in .net

  1. Mohammed Feroz July 27, 2008 at 11:36 am

    hi
    Perfect .. its gr8 code. its solved my problem.

    thank’x

    best regards
    feroz

  2. Sara December 9, 2008 at 3:47 pm

    Thank you,
    It solved my problem 🙂

  3. Hari February 17, 2009 at 10:17 am

    It’s a cool solution 🙂

  4. Bean March 11, 2009 at 9:59 pm

    Another Solution:
    ===============

    Public Shared Function removeDuplicates(ByVal myArray As Array)
    Dim i As Integer
    Dim stringList As String = “”
    Dim firstValue As String = “”
    Dim lastValue As String = “”
    Dim newArray As Array

    Array.Sort(myArray)

    For i = 0 To myArray.Length – 1
    firstValue = CStr(myArray(i))

    If UCase(firstValue) UCase(lastValue) Then
    If stringList.Length = 0 Then
    stringList += myArray(i)
    Else
    stringList += “,” & myArray(i)
    End If
    End If
    lastValue = myArray(i)
    Next i

    newArray = Split(stringList, “,”)

    Return newArray
    End Function

  5. farogh haider June 30, 2009 at 7:16 am

    Thanks,

    It’s working fine.

  6. martha September 14, 2009 at 8:58 pm

    Im sorry, but in c# I not solved my problem, please help me!! thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: