?
Solved

How Do you Remove Duplicates?

Posted on 2004-11-04
7
Medium Priority
?
948 Views
Last Modified: 2010-04-24
How would you? Write a sub called removeDuplicates.  This function receives a listbox.  The function alters the contents of the listbox so that any values that were in the listbox more than once are removed so that each value is unique.  NOTE : The order of the items in the listbox should not be altered.
0
Comment
Question by:dblanch12
7 Comments
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 12499789
this sounds like homework which is against EE policy.
0
 
LVL 8

Expert Comment

by:wguerram
ID: 12499835
Try this code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim _hashTable As New Hashtable

        With ListBox1.Items
            For i As Integer = .Count - 1 To 0 Step -1
                If _hashTable.ContainsKey(.Item(i)) Then
                    .RemoveAt(i)
                Else
                    _hashTable.Add(.Item(i), .Item(i))
                End If
            Next
        End With

        _hashTable.Clear()
        _hashTable = Nothing
    End Sub
0
 
LVL 28

Expert Comment

by:iboutchkine
ID: 12504060
' Check for Duplicates
    ' Search listbox (from last to first)
    For cntr = lbDwgList.Items.Count - 1 To 1 Step -1
      ' If next item is a duplicate -> Remove It
      If lbDwgList.Items(cntr) = lbDwgList.Items(cntr - 1) Then _
         lbDwgList.Items.RemoveAt(cntr)
    Next
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 12504349
wguerram,

Part of the requirements was that the "order of the items in the listbox should not be altered.".  If we have this in the listbox:

    cat
    dog
    fish
    dog
    cat

Your code will produce:

    fish
    dog
    cat

When it should be:

    cat
    dog
    fish

This will preserve the order of the items while removing duplicates:

        Dim HT As New Hashtable
        Dim i As Integer

        If ListBox1.Items.Count > 0 Then
            While i < ListBox1.Items.Count
                If HT.ContainsKey(ListBox1.Items(i)) Then
                    ListBox1.Items.RemoveAt(i)
                Else
                    HT.Add(ListBox1.Items(i), ListBox1.Items(i))
                    i = i + 1
                End If
            End While

            HT = Nothing
        End If
0
 
LVL 8

Expert Comment

by:wguerram
ID: 12504377
iboutchkine your code assumes the list is ordered. if so your code would be the way to go otherwise he had to used the code i proposed.
0
 
LVL 8

Expert Comment

by:wguerram
ID: 12504498
Well Idle_Mind my code does not alter the order of the items.

I am just starting from down to up and you did it from up to down.

Now if the list is ordered descending my code would produce the right order and your not.

that will depend on how  dblanch12 looks it.

i am just removing the items, i didn't altered the oreder.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12504743
My code will keep descending items in order as well but...

It doesn't matter if the list is sorted ascending or descending or some kind of custom sorting.  Keeping the order means top to bottom since things at the top occur before things at the bottom.

So you have to go from top to bottom so that when you encounter a duplicate you are removing the duplicate, thus keeping the first occurence in the position it was found at.

By traversing bottom to top, you are actually keeping the last occurence of a duplicated item in the list and removing all others that occurred before it.

~IM
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
SQL Database Recovery Software repairs the MDF & NDF Files, corrupted due to hardware related issues or software related errors. Provides preview of recovered database objects and allows saving in either MSSQL, CSV, HTML or XLS format. Ensures recov…
Suggested Courses
Course of the Month9 days, 1 hour left to enroll

621 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