remove duplicates entries from string array

Hi Experts
i have the following string array from which i need to remove duplicate entries first and then sort it alphabatically.

Dim strArray() As String =
("Camaguey","Havana","Varadero","La Romana","Punta Cana","Santo Domingo","Havana","Varadero","Antigua","Nassau","Bridgetown","Guanacaste","Liberia","San jose","Cayo Coco","Cayo Largo","Havana","Holguin","Santa Clara","Varadero","Cabarete","La Romana","Puerto Plata","Punta Cana","Samana","Santo Domingo","Sosua","Montego Bay","Cancun","Cozumel","Ixtapa","Manzanillo","Mayan riviera (Playa del Carmen)","Mazatlan","Puerto
Vallarta","San Jose del Cabo","St Maarten","Providenciales","Honolulu","Las Vegas","Maui","Holguin","Varadero","Punta Cana","Montego Bay","Cancun","Mayan riviera (Playa del Carmen)","St Maarten","Antigua","Aruba","Nassau","Bridgetown","Bermuda","Grand cayman","Guanacaste","Liberia","San jose","Cayo Coco","Cayo Largo","Havana","Holguin","Santa
Clara","Varadero","Cabarete","La Romana","Puerto Plata","Montego Bay","Cancun","Cozumel","Huatulco","Ixtapa","Manzanillo","Mayan riviera (Playa del armen)","Mazatlan","Puerto Vallarta","San Jose del Cabo","Amsterdam","St Maarten","Providenciales","Honolulu","Las Vegas","Maui","Antigua","Aruba","Nassau","Bridgetown")

Please write me the working sample that fits my need. Thanks alot for great favour

Best Regards
Shaukat
shwaqar82Asked:
Who is Participating?
 
PaulHewsCommented:
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim strArray() As String = _
        {"Camaguey", "Havana", "Varadero", "La Romana", "Punta Cana", "Santo Domingo", "Havana", _
        "Varadero", "Antigua", "Nassau", "Bridgetown", "Guanacaste", "Liberia", "San jose", "Cayo Coco", _
        "Cayo Largo", "Havana", "Holguin", "Santa Clara", "Varadero", "Cabarete", "La Romana", "Puerto Plata", _
        "Punta Cana", "Samana", "Santo Domingo", "Sosua", "Montego Bay", "Cancun", "Cozumel", "Ixtapa", _
        "Manzanillo", "Mayan riviera (Playa del Carmen)", "Mazatlan", "Puerto Vallarta", _
        "San Jose del Cabo", "St Maarten", "Providenciales", "Honolulu", "Las Vegas", "Maui", "Holguin", "Varadero", _
        "Punta Cana", "Montego Bay", "Cancun", "Mayan riviera (Playa del Carmen)", "St Maarten", "Antigua", "Aruba", _
        "Nassau", "Bridgetown", "Bermuda", "Grand cayman", "Guanacaste", "Liberia", "San jose", "Cayo Coco", _
        "Cayo Largo", "Havana", "Holguin", "Santa Clara", "Varadero", "Cabarete", "La Romana", "Puerto Plata", _
        "Montego Bay", "Cancun", "Cozumel", "Huatulco", "Ixtapa", "Manzanillo", "Mayan riviera (Playa del armen)", _
        "Mazatlan", "Puerto Vallarta", "San Jose del Cabo", "Amsterdam", "St Maarten", "Providenciales", "Honolulu", _
        "Las Vegas", "Maui", "Antigua", "Aruba", "Nassau", "Bridgetown"}
        Dim xList As New List(Of String)


        Dim Singles() As String = RemoveDups(strArray)
        For i As Integer = 0 To Singles.GetUpperBound(0)
            Debug.WriteLine(Singles(i))
        Next


    End Sub

    Private Function RemoveDups(ByVal ArrayString() As String) As String()
        Dim ListString As New List(Of String)
        Array.Sort(ArrayString)
        For i As Integer = 0 To ArrayString.GetUpperBound(0) - 1
            If ArrayString(i) <> ArrayString(i + 1) Then
                ListString.Add(ArrayString(i))
            End If
        Next
        Return ListString.ToArray
    End Function

End Class
0
 
DhaestCommented:
   Dim arr As New ArrayList
       Dim strArray() As String =
("Camaguey","Havana","Varadero","La Romana","Punta Cana","Santo Domingo","Havana","Varadero","Antigua","Nassau","Bridgetown","Guanacaste","Liberia","San jose","Cayo Coco","Cayo Largo","Havana","Holguin","Santa Clara","Varadero","Cabarete","La Romana","Puerto Plata","Punta Cana","Samana","Santo Domingo","Sosua","Montego Bay","Cancun","Cozumel","Ixtapa","Manzanillo","Mayan riviera (Playa del Carmen)","Mazatlan","Puerto
Vallarta","San Jose del Cabo","St Maarten","Providenciales","Honolulu","Las Vegas","Maui","Holguin","Varadero","Punta Cana","Montego Bay","Cancun","Mayan riviera (Playa del Carmen)","St Maarten","Antigua","Aruba","Nassau","Bridgetown","Bermuda","Grand cayman","Guanacaste","Liberia","San jose","Cayo Coco","Cayo Largo","Havana","Holguin","Santa
Clara","Varadero","Cabarete","La Romana","Puerto Plata","Montego Bay","Cancun","Cozumel","Huatulco","Ixtapa","Manzanillo","Mayan riviera (Playa del armen)","Mazatlan","Puerto Vallarta","San Jose del Cabo","Amsterdam","St Maarten","Providenciales","Honolulu","Las Vegas","Maui","Antigua","Aruba","Nassau","Bridgetown")

       For i As Integer = 0 To ar.Length - 1
           If Not arr.Contains(ar.GetValue(i)) Then
               arr.Add(ar.GetValue(i))
           End If
       Next
       ar = arr.ToArray(GetType(Integer))
0
 
VBRocksCommented:
I would like to recommend using a DataTable.  It's very easy, fast and efficient:

        Dim dt As New DataTable("Counties")
        dt.Columns.Add("Country")
        For Each country As String In strArray
            dt.Rows.Add(New String() {country})
        Next

        'Group all of the duplicate rows together and load into tableDistinct
        '    There will be no duplicates in this table
        Dim tableDistinct As DataTable = _
            dt.DefaultView.ToTable(True, New String() {"Country"})

        '*** We are done. ***



        'To see the rows sorted in the Output Window:
        Dim dv As DataView = tableDistinct.DefaultView
        dv.Sort = "Country ASC"
        For Each row As DataRowView In dv
            Debug.WriteLine(row.Item("Country"))
        Next

        Stop

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.