Results 1 to 5 of 5

Thread: Removing duplicates ?

  1. #1

    Thread Starter
    Frenzied Member thegreatone's Avatar
    Join Date
    Aug 2003
    Location
    Oslo, Norway. Mhz:4800 x12
    Posts
    1,333

    Removing duplicates ?

    ok, now you here, How would i go about removing duplicates from a Listbox ?

    i want to do this when a button is clicked, this is basically because when i parse a particular page the first result duplicates itself, so i want to remove any duplicates...

    Any help is appreciated !
    Zeegnahtuer?

  2. #2
    Addicted Member Bregalad's Avatar
    Join Date
    Jul 2000
    Location
    Oslo,Norway
    Posts
    183
    The following code will remove duplicates in List1.
    It may be a bit slow if listcount is high...

    VB Code:
    1. Private colList As Collection
    2.  
    3. Private Sub cmdRemDuplicate_Click()
    4.    Dim nC As Integer
    5.    Set colList = New Collection
    6.    For nC = 0 To List1.ListCount - 1
    7.       Call AddToCollection(List1.List(nC))
    8.    Next nC
    9.    List1.Clear
    10.    For nC = 1 To colList.Count
    11.       Call List1.AddItem(colList(nC))
    12.    Next nC
    13. End Sub
    14.  
    15. Private Sub Form_Load()
    16.    List1.AddItem "Test1"
    17.    List1.AddItem "Test1"
    18.    List1.AddItem "An error also occurs if a specified key duplicates the key for an existing member of the collection."
    19.    List1.AddItem "An error also occurs if a specified key duplicates the key for an existing member of the collection."
    20.    List1.AddItem "Test3"
    21.    List1.AddItem "Test4"
    22.    List1.AddItem "Test4"
    23. End Sub
    24.  
    25. Private Sub AddToCollection(ByVal sText As String)
    26.    On Error GoTo Duplicate
    27.    Call colList.Add(sText, sText)
    28.    Exit Sub
    29. Duplicate:
    30.    'Item already exists, won't be added to collection
    31. End Sub

  3. #3
    Addicted Member Bregalad's Avatar
    Join Date
    Jul 2000
    Location
    Oslo,Norway
    Posts
    183

    hmm.. faster / better

    VB Code:
    1. Private colList As Collection
    2.  
    3. Private Sub cmdRemDuplicate_Click()
    4.    Dim nC As Integer
    5.    Set colList = New Collection
    6.    While nC < List1.ListCount
    7.       If IsDuplicate(List1.List(nC)) Then
    8.          Call List1.RemoveItem(nC)
    9.       Else
    10.          nC = nC + 1
    11.       End If
    12.    Wend
    13. End Sub
    14.  
    15. Private Sub Form_Load()
    16.    List1.AddItem "Test1"
    17.    List1.AddItem "Test1"
    18.    List1.AddItem "An error also occurs if a specified key duplicates the key for an existing member of the collection."
    19.    List1.AddItem "An error also occurs if a specified key duplicates the key for an existing member of the collection."
    20.    List1.AddItem "Test3"
    21.    List1.AddItem "Test4"
    22.    List1.AddItem "Test4"
    23. End Sub
    24.  
    25. Private Function IsDuplicate(ByVal sText As String) As Boolean
    26.    On Error GoTo Duplicate
    27.    Call colList.Add(sText, sText)
    28.    IsDuplicate = False
    29.    Exit Function
    30. Duplicate:
    31.    IsDuplicate = True
    32. End Function

  4. #4
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Using a dictionary is the fastest method in VB.
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  5. #5
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width