How Do you Remove Duplicates?

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.
dblanch12Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gregoryyoungCommented:
this sounds like homework which is against EE policy.
0
wguerramCommented:
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
iboutchkineCommented:
' 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
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

Mike TomlinsonMiddle School Assistant TeacherCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
wguerramCommented:
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
wguerramCommented:
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
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

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.