To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
VBForums  

VB Wire News
Article :: Building Dynamic Systems with Expressions in .NET
How Is XML Like An Interface?
Understanding Covariance and Contravariance
Print VS 2010 Keyboard Shortcut References in Letter (8.5x11in) and A4 (210×297mm) Sizes
Updated Productivity Power Tools



Go Back   VBForums > VBForums CodeBank > CodeBank - Visual Basic 6 and earlier

Reply Post New Thread
 
Thread Tools Display Modes
Old Apr 2nd, 2002, 04:21 AM   #1
plenderj
Banned
 
plenderj's Avatar
 
Join Date: Jan 01
Location: Dublin, Ireland
Posts: 10,359
plenderj  is on a distinguished road (40+)
Removing duplicates from an array

How to remove duplicated items in an array.
This is (in my opinion) a very fast way of doing it.

I don't know of any faster methods.
If you need to remove duplicated items from an array of a different type, then just adjust the code accordingly.

Note: You will need to add a reference to "Microsoft Scripting Runtime" as the code uses its Dictionary object.
To do this, select Project from the toolbar, then select "References", and then select "Microsoft Scripting Runtime"

VB Code:
  1. Option Explicit
  2. Private Sub removeDuplicates(ByRef arrName() As Long)
  3.     Dim i As Long, tempArr() As Long: ReDim tempArr(UBound(arrName))
  4.     Dim d As New Dictionary, n As Long
  5.     For i = 0 To UBound(arrName)
  6.         If Not d.Exists(arrName(i)) Then
  7.             d.Add arrName(i), arrName(i)
  8.             tempArr(n) = arrName(i): n = n + 1
  9.         End If
  10.     Next
  11.     ReDim Preserve tempArr(n)
  12.     arrName = tempArr
  13. End Sub
  14. Private Sub Form_Load()
  15.     Dim x() As Long, i As Long: ReDim x(99)
  16.    
  17.     '' this loop will fill the array with values :
  18.     '' 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3...
  19.     ''
  20.     For i = 0 To 99
  21.         x(i) = i Mod 4
  22.     Next
  23.    
  24.     '' now remove the duplicated items
  25.     ''
  26.     removeDuplicates x
  27.    
  28.     '' now display whats left
  29.     For i = 0 To UBound(x) - 1
  30.         Debug.Print i & ":" & x(i)
  31.     Next
  32. End Sub

Last edited by plenderj; Apr 2nd, 2002 at 04:24 AM.
plenderj is offline   Reply With Quote
Old Oct 21st, 2004, 07:42 AM   #2
plenderj
Banned
 
plenderj's Avatar
 
Join Date: Jan 01
Location: Dublin, Ireland
Posts: 10,359
plenderj  is on a distinguished road (40+)
I still believe this to be the fastest method of removing duplicated items available in Classic VB.
plenderj is offline   Reply With Quote
Old Oct 21st, 2004, 08:45 AM   #3
plenderj
Banned
 
plenderj's Avatar
 
Join Date: Jan 01
Location: Dublin, Ireland
Posts: 10,359
plenderj  is on a distinguished road (40+)
* 21-October-2004 - Moved to CodeBank *
plenderj is offline   Reply With Quote
Old Oct 31st, 2004, 06:09 AM   #4
alawra
Junior Member
 
Join Date: Oct 04
Location: world
Posts: 19
alawra is an unknown quantity at this point (<10)
how to use it in text please
alawra is offline   Reply With Quote
Old Oct 31st, 2004, 11:27 AM   #5
plenderj
Banned
 
plenderj's Avatar
 
Join Date: Jan 01
Location: Dublin, Ireland
Posts: 10,359
plenderj  is on a distinguished road (40+)
Just modify the code above to filter out Strings instead of Longs.
plenderj is offline   Reply With Quote
Old Dec 30th, 2004, 10:36 PM   #6
Merri
VB6, XHTML & CSS hobbyist
 
Merri's Avatar
 
Join Date: Oct 02
Location: Finland
Posts: 6,330
Merri is a splendid one to behold (700+)Merri is a splendid one to behold (700+)Merri is a splendid one to behold (700+)Merri is a splendid one to behold (700+)Merri is a splendid one to behold (700+)Merri is a splendid one to behold (700+)Merri is a splendid one to behold (700+)Merri is a splendid one to behold (700+)
Re: Removing duplicates from an array

Quote:
Originally Posted by plenderj
I still believe this to be the fastest method of removing duplicated items available in Classic VB.
You don't need to believe anymore: it isn't


This one is faster with strings by 10 - 20%
VB Code:
  1. Public Sub strArrRemoveDuplicate(ByRef StringArray() As String)
  2.     Dim LowBound As Long, UpBound As Long
  3.     Dim TempArray() As String, Cur As Long
  4.     Dim A As Long, B As Long
  5.    
  6.     'check for empty array
  7.     If (Not StringArray) = True Then Exit Sub
  8.    
  9.     'we need these often
  10.     LowBound = LBound(StringArray)
  11.     UpBound = UBound(StringArray)
  12.    
  13.     'reserve check buffer
  14.     ReDim TempArray(LowBound To UpBound)
  15.    
  16.     'set first item
  17.     Cur = LowBound
  18.     TempArray(Cur) = StringArray(LowBound)
  19.    
  20.     'loop through all items
  21.     For A = LowBound + 1 To UpBound
  22.         'make a comparison against all items
  23.         For B = LowBound To Cur
  24.             'if is a duplicate, exit array
  25.             If LenB(TempArray(B)) = LenB(StringArray(A)) Then
  26.                 If InStrB(1, StringArray(A), TempArray(B), vbBinaryCompare) = 1 Then Exit For
  27.             End If
  28.         Next B
  29.         'check if the loop was exited: add new item to check buffer if not
  30.         If B > Cur Then Cur = B: TempArray(Cur) = StringArray(A)
  31.     Next A
  32.    
  33.     'fix size
  34.     ReDim Preserve TempArray(LowBound To Cur)
  35.     'copy
  36.     StringArray = TempArray
  37. End Sub


This works for Byte, Integer and Long datatypes and is four to five times faster:
VB Code:
  1. Public Sub bArrRemoveDuplicate(ByRef ByteArray() As Byte)
  2.     Dim LowBound As Long, UpBound As Long
  3.     Dim TempArray() As Byte, TempByte As Byte, Cur As Long
  4.     Dim A As Long, B As Long
  5.    
  6.     'check for empty array
  7.     If (Not ByteArray) = True Then Exit Sub
  8.    
  9.     'we need these often
  10.     LowBound = LBound(ByteArray)
  11.     UpBound = UBound(ByteArray)
  12.    
  13.     'reserve check buffer
  14.     ReDim TempArray(LowBound To UpBound)
  15.    
  16.     'set first item
  17.     Cur = LowBound
  18.     TempArray(Cur) = ByteArray(LowBound)
  19.    
  20.     'loop through all items
  21.     For A = LowBound + 1 To UpBound
  22.         TempByte = ByteArray(A)
  23.         'make a comparison against all items
  24.         For B = LowBound To Cur
  25.             'if is a duplicate, exit array
  26.             If (TempArray(B) Xor TempByte) = 0 Then Exit For
  27.         Next B
  28.         'check if the loop was exited: add new item to check buffer if not
  29.         If B > Cur Then Cur = B: TempArray(Cur) = ByteArray(A)
  30.     Next A
  31.    
  32.     'fix size
  33.     ReDim Preserve TempArray(LowBound To Cur)
  34.     'copy
  35.     ByteArray = TempArray
  36. End Sub

To convert it to use Long for example, just use VB's inbuilt replace from the edit menu and make it convert Byte to Long. And rename the function, of course


What is the best thing with these functions: you don't need to add any extra reference to your project!
__________________
Unicode classes, functions... in Visual Basic 6

VB6 in occasional use. I'm mostly HTML, CSS & JavaScript these days.
« Antec Sonata II: Core 2 Duo E7400, ASRock P45TS, Asus EN9600GT 512 MB, 4 GB, 1.25 TB »
« OS: Windows 7 | Laptop: Amilo Pi 2530-12P| Netbook: Asus EEE 901 »

Last edited by Merri; Dec 30th, 2004 at 10:56 PM.
Merri is offline   Reply With Quote
Old Jan 10th, 2005, 02:41 AM   #7
plenderj
Banned
 
plenderj's Avatar
 
Join Date: Jan 01
Location: Dublin, Ireland
Posts: 10,359
plenderj  is on a distinguished road (40+)
Re: Removing duplicates from an array

Can you post the code you used to compare the times, because in the brief test I did my code still worked faster...
plenderj is offline   Reply With Quote
Old Jan 14th, 2005, 06:08 PM   #8
Maven
Hyperactive Member
 
Maven's Avatar
 
Join Date: Feb 03
Location: Greeneville, TN
Posts: 274
Maven is on a distinguished road (20+)
Re: Removing duplicates from an array

__________________
Education is an admirable thing, but it is well to remember from time to time that nothing that is worth knowing can be taught. - Oscar Wilde

My little blog: http://vblore.blogspot.com/
Maven is offline   Reply With Quote
Old Jan 24th, 2005, 04:54 AM   #9
plenderj
Banned
 
plenderj's Avatar
 
Join Date: Jan 01
Location: Dublin, Ireland
Posts: 10,359
plenderj  is on a distinguished road (40+)
Re: Removing duplicates from an array

Ah I take it you're comparing my code to your ASM code?
plenderj is offline   Reply With Quote
Old Jan 24th, 2005, 11:10 AM   #10
Maven
Hyperactive Member
 
Maven's Avatar
 
Join Date: Feb 03
Location: Greeneville, TN
Posts: 274
Maven is on a distinguished road (20+)
Re: Removing duplicates from an array

Quote:
Originally Posted by plenderj
Ah I take it you're comparing my code to your ASM code?
Na, he was comparing my asm code, you're code, with his code.
__________________
Education is an admirable thing, but it is well to remember from time to time that nothing that is worth knowing can be taught. - Oscar Wilde

My little blog: http://vblore.blogspot.com/
Maven is offline   Reply With Quote
Old Nov 10th, 2009, 07:44 AM   #11
oli12345
New Member
 
Join Date: Nov 09
Posts: 1
oli12345 is an unknown quantity at this point (<10)
Re: Removing duplicates from an array

How about this compared to your methods? How are you comparing the speeds?

Function removeDuplicates(ByVal initialArray As String()) As String()
Dim i As Integer = 0
Dim j As Integer = 0
Dim newArray(0) As String

For i = 0 To UBound(initialArray)
For j = 0 To UBound(initialArray)
If Not initialArray(i) = "" Then
If Not j = i Then
If initialArray(i) = initialArray(j) Then
initialArray(j) = ""
End If
End If
End If
Next
Next

j = 0
For i = 0 To UBound(initialArray)
If Not initialArray(i) = "" Then
ReDim Preserve newArray(j)
newArray(j) = initialArray(i)
j = j + 1
End If
Next

Return newArray

End Function
oli12345 is offline   Reply With Quote
Reply

Go Back   VBForums > VBForums CodeBank > CodeBank - Visual Basic 6 and earlier


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 07:54 PM.





Acceptable Use Policy

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.