?
Solved

remove duplicates entries from string array

Posted on 2007-08-06
3
Medium Priority
?
389 Views
Last Modified: 2013-11-07
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
0
Comment
Question by:shwaqar82
3 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 19640343
   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
 
LVL 38

Accepted Solution

by:
PaulHews earned 2000 total points
ID: 19640362
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
 
LVL 27

Expert Comment

by:VBRocks
ID: 19640676
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

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Loops Section Overview

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question