Results 1 to 11 of 11

Thread: Removing duplicates from an array

  1. #1
    Banned plenderj's Avatar
    Join Date
    Jan 01
    Location
    Dublin, Ireland
    Posts
    10,359

    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.  
    3. Private Sub removeDuplicates(ByRef arrName() As Long)
    4.     Dim i As Long, tempArr() As Long: ReDim tempArr(UBound(arrName))
    5.     Dim d As New Dictionary, n As Long
    6.     For i = 0 To UBound(arrName)
    7.         If Not d.Exists(arrName(i)) Then
    8.             d.Add arrName(i), arrName(i)
    9.             tempArr(n) = arrName(i): n = n + 1
    10.         End If
    11.     Next
    12.     ReDim Preserve tempArr(n)
    13.     arrName = tempArr
    14. End Sub
    15.  
    16. Private Sub Form_Load()
    17.     Dim x() As Long, i As Long: ReDim x(99)
    18.    
    19.     '' this loop will fill the array with values :
    20.     '' 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3...
    21.     ''
    22.     For i = 0 To 99
    23.         x(i) = i Mod 4
    24.     Next
    25.    
    26.     '' now remove the duplicated items
    27.     ''
    28.     removeDuplicates x
    29.    
    30.     '' now display whats left
    31.     For i = 0 To UBound(x) - 1
    32.         Debug.Print i & ":" & x(i)
    33.     Next
    34. End Sub
    Last edited by plenderj; Apr 2nd, 2002 at 04:24 AM.

  2. #2
    Banned plenderj's Avatar
    Join Date
    Jan 01
    Location
    Dublin, Ireland
    Posts
    10,359
    I still believe this to be the fastest method of removing duplicated items available in Classic VB.

  3. #3
    Banned plenderj's Avatar
    Join Date
    Jan 01
    Location
    Dublin, Ireland
    Posts
    10,359
    * 21-October-2004 - Moved to CodeBank *

  4. #4
    Junior Member
    Join Date
    Oct 04
    Location
    world
    Posts
    19
    how to use it in text please

  5. #5
    Banned plenderj's Avatar
    Join Date
    Jan 01
    Location
    Dublin, Ireland
    Posts
    10,359
    Just modify the code above to filter out Strings instead of Longs.

  6. #6
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 02
    Location
    Finland
    Posts
    6,653

    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.  
    21.  
    22.     'loop through all items
    23.     For A = LowBound + 1 To UpBound
    24.         'make a comparison against all items
    25.         For B = LowBound To Cur
    26.             'if is a duplicate, exit array
    27.             If LenB(TempArray(B)) = LenB(StringArray(A)) Then
    28.                 If InStrB(1, StringArray(A), TempArray(B), vbBinaryCompare) = 1 Then Exit For
    29.             End If
    30.         Next B
    31.         'check if the loop was exited: add new item to check buffer if not
    32.         If B > Cur Then Cur = B: TempArray(Cur) = StringArray(A)
    33.     Next A
    34.    
    35.     'fix size
    36.     ReDim Preserve TempArray(LowBound To Cur)
    37.     'copy
    38.     StringArray = TempArray
    39. 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!
    Last edited by Merri; Dec 30th, 2004 at 10:56 PM.

  7. #7
    Banned plenderj's Avatar
    Join Date
    Jan 01
    Location
    Dublin, Ireland
    Posts
    10,359

    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...

  8. #8
    Hyperactive Member Maven's Avatar
    Join Date
    Feb 03
    Location
    Greeneville, TN
    Posts
    274

    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

  9. #9
    Banned plenderj's Avatar
    Join Date
    Jan 01
    Location
    Dublin, Ireland
    Posts
    10,359

    Re: Removing duplicates from an array

    Ah I take it you're comparing my code to your ASM code?

  10. #10
    Hyperactive Member Maven's Avatar
    Join Date
    Feb 03
    Location
    Greeneville, TN
    Posts
    274

    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

  11. #11
    New Member
    Join Date
    Nov 09
    Posts
    1

    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

Posting Permissions

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