VB icon

A Better, Faster, Remove Duplicates Function (Kill Dupes, Remove Dupes, Kill Duplicates)

Email
Submitted on: 2/13/2015 7:50:00 AM
By: Chad Roe (from psc cd)  
Level: Beginner
User Rating: By 3 Users
Compatibility: VB 3.0, VB 4.0 (16-bit), VB 4.0 (32-bit), VB 5.0, VB 6.0
Views: 764
 
     Please check "http://chad.offroadextremes.com/forums" for all my source code and projects. This is a very fast, very efficient RemoveDupes Function. Every single RemoveDupes Sub/Function I have ever seen on PSCode has been very slow and never removes all the duplicate list items. If the List/Combo contained more than one duplicate, only the first duplicate was removed. My Function, however, removes every single duplicate no matter how many there are. It also does it very quickly without going back through the list. It only sweeps through the list once. This is the fastest and most efficient way of removing dupes in VB. Also, I have added the options to remove all matches no matter the Case or if it has spaces or not, remove matches that only match exactly or any combination of matching options. As an added bonus, I wrote it as a Function to return the number of duplicates removed. You can call this Function like you would a sub for simplicity, or use anything to display the number of dupes removed. EXAMPLES: [1] Call RemoveDupes(List1, True, True) [2] Call MsgBox(RemoveDupes(List1, True, True)) [3] lngDupes& = Call RemoveDupes(List1, True, True)

 

Windows API/Global Declarations:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
'**************************************
'Windows API/Global Declarations for :A Better, Faster,Remove Duplicates Function (Kill Dupes, Remove Dupes, Kill Duplicates)
'**************************************
None.
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: A Better, Faster,Remove Duplicates Function (Kill Dupes, Remove Dupes, Kill Duplicates)
' Description:Please check "http://chad.offroadextremes.com/forums" for all my source code and projects. This is a very fast, very efficient RemoveDupes Function. Every single RemoveDupes Sub/Function I have ever seen on PSCode has been very slow and never removes all the duplicate list items. If the List/Combo contained more than one duplicate, only the first duplicate was removed. My Function, however, removes every single duplicate no matter how many there are. It also does it very quickly without going back through the list. It only sweeps through the list once. This is the fastest and most efficient way of removing dupes in VB. Also, I have added the options to remove all matches no matter the Case or if it has spaces or not, remove matches that only match exactly or any combination of matching options. As an added bonus, I wrote it as a Function to return the number of duplicates removed. You can call this Function like you would a sub for simplicity, or use anything to display the number of dupes removed. EXAMPLES: [1] Call RemoveDupes(List1, True, True) [2] Call MsgBox(RemoveDupes(List1, True, True)) [3] lngDupes& = Call RemoveDupes(List1, True, True)
' By: Chad Roe (from psc cd)
'
' Inputs:lstList = The List you are removing dupes from. blnLowerCase = Wether you want to match LCase or not. blnNoSpaces = Wether you want to match with or without spaces. Setting both to True will remove all duplicates that are the same no matter what Case they are or if they contain spaces or not. EXAMPLE: "aA a" is the same as "aaa" if both are set to True. "aA a" and "aaa" are different if both are set to False.
'
' Returns:Returns a ListBox or ComboBox with no duplicate items.
'
' Assumes:As you can see in the code, after an item is removed, I set the current Check Item back one to make up for the item just removed. This is why this Function removes ALL duplicate items and not just the first one. Also, I do not use DoEvents because this drastically slows down the process. To me, this is the greatest Dupe Killer that can be written in Visual Basic.
'
' Side Effects:None.
'**************************************

Public Function RemoveDupes(lstList As Control, blnLowerCase As Boolean, blnNoSpaces As Boolean) As Long
Dim strItem As String, strCheck As String
Dim intItem As Integer, intCheck As Integer
RemoveDupes& = lstList.ListCount
For intItem% = 0 To lstList.ListCount - 1
 For intCheck% = intItem% + 1 To lstList.ListCount - 1
strItem$ = lstList.List(intItem%)
If blnLowerCase = True Then strItem$ = LCase(strItem$)
If blnNoSpaces = True Then strItem$ = Replace(strItem$, " ", "")
strCheck$ = lstList.List(intCheck%)
If blnLowerCase = True Then strCheck$ = LCase(strCheck$)
If blnNoSpaces = True Then strCheck$ = Replace(strCheck$, " ", "")
If strItem$ = strCheck$ Then
 Call lstList.RemoveItem(intCheck%)
 intCheck% = intCheck% - 1
End If
 Next intCheck%
Next intItem%
RemoveDupes& = RemoveDupes& - lstList.ListCount
End Function


Other 3 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.