使用vb6更快地删除重复项

使用vb6更快地删除重复项,vb6,Vb6,我有这个功能,在vb6中删除重复项的速度很慢 Function FilterDuplicates(Arr As Variant) As Long Dim col As Collection, index As Long, dups As Long Set col = New Collection On Error Resume Next For index = LBound(Arr) To UBound(Arr) ' build th

我有这个功能,在vb6中删除重复项的速度很慢

Function FilterDuplicates(Arr As Variant) As Long
    Dim col      As Collection, index As Long, dups As Long
    Set col = New Collection

    On Error Resume Next

    For index = LBound(Arr) To UBound(Arr)
        ' build the key using the array element
        ' an error occurs if the key already exists
        col.Add 0, CStr(Arr(index))
        If Err Then
            ' we've found a duplicate
            Arr(index) = Empty
            dups = dups + 1
            Err.Clear
        ElseIf dups Then
            ' if we've found one or more duplicates so far
            ' we need to move elements towards lower indices
            Arr(index - dups) = Arr(index)
            Arr(index) = Empty
        End If
    Next

    ' return the number of duplicates
    FilterDuplicates = dups

End Function

我需要优化此函数以加快运行速度,请使用字符串连接(对于大数组不要太快)和InStrB()函数帮助:

Function FilterDuplicates(Arr As Variant) As Long
    Dim col      As Dictionary, index As Long, dups As Long
    Set col = New Dictionary

    On Error Resume Next

    For index = LBound(Arr) To UBound(Arr)
        ' build the key using the array element
        ' an error occurs if the key already exists
        If col.Exists(Arr(index)) Then
            ' we've found a duplicate
            dups = dups + 1
        Else
            Call col.Add(Arr(index), vbNullstring)
        End If
    Next

    Dim newArr(1 to col.Keys.Count) As Variant
    Dim newIndex As Long
    For index = LBound(Arr) To UBound(Arr)
        If col(Arr(index)) = vbNullstring Then
            newIndex = newIndex + 1
            col(Arr(index)) = "Used"
            newArr(newIndex) = Arr(index)
        End If
    Next index
    Arr = newArr

    ' return the number of duplicates
    FilterDuplicates = dups

End Function
Function FilterDuplicates(arr As Variant) As Long
Dim item As String, dups As Long, strArray As String

For i = LBound(arr) To UBound(arr)
    item = arr(i)
    If lenb(item) <> 0 Then
      If InStrB(1, strArray, item) = 0 Then
        strArray = strArray & item & ";"
      Else
        dups = dups + 1
      End If
    End If
Next i

FilterDuplicates = dups
End Function
函数过滤器(arr作为变量)的复制长度为
将项目变暗为字符串,将重复项变长,将字符串变粗
对于i=LBound(arr)到UBound(arr)
项目=arr(i)
如果lenb(项目)为0,则
如果InStrB(1,strArray,item)=0,则
strArray=strArray&item&“;”
其他的
dups=dups+1
如果结束
如果结束
接下来我
FilterDuplicates=dups
端函数

使用字符串连接(对于大数组,不要太快)和InStrB()函数:

Function FilterDuplicates(arr As Variant) As Long
Dim item As String, dups As Long, strArray As String

For i = LBound(arr) To UBound(arr)
    item = arr(i)
    If lenb(item) <> 0 Then
      If InStrB(1, strArray, item) = 0 Then
        strArray = strArray & item & ";"
      Else
        dups = dups + 1
      End If
    End If
Next i

FilterDuplicates = dups
End Function
函数过滤器(arr作为变量)的复制长度为
将项目变暗为字符串,将重复项变长,将字符串变粗
对于i=LBound(arr)到UBound(arr)
项目=arr(i)
如果lenb(项目)为0,则
如果InStrB(1,strArray,item)=0,则
strArray=strArray&item&“;”
其他的
dups=dups+1
如果结束
如果结束
接下来我
FilterDuplicates=dups
端函数

功能不完整,我该如何使用它?两件事。首先,您必须使用“项目/引用”对话框将对“Microsoft脚本运行时”的引用添加到项目中,以便使用字典。第二,如果列存在,则
行中缺少
。就其不完整性而言,它所缺少的只是在For/Next完成后重建数组,不包括
col
中存在的那些项。我想你能处理好的。。。?提示:另一个For/Next循环?我不能用普通数组代替集合吗?@Oghenero-我以为你想让它快一点。字典是基于哈希表的,所以它有O(1)个查找时间。因此,查找重复项很快。要在数组中查找重复项,它必须扫描数组,这是一个O(n)操作。由于必须在循环内执行重复项检查,因此基于字典的完整解决方案为O(n),而基于数组的解决方案为O(n^2)—这是错误的。@Oghenero—字典是哈希表的实现。我认为“如何在VB6中实现哈希表?”将是它自己的问题。但我会帮你:这个函数不完整,那么我该如何使用它呢?两件事。首先,您必须使用“项目/引用”对话框将对“Microsoft脚本运行时”的引用添加到项目中,以便使用字典。第二,如果列存在,则
行中缺少
。就其不完整性而言,它所缺少的只是在For/Next完成后重建数组,不包括
col
中存在的那些项。我想你能处理好的。。。?提示:另一个For/Next循环?我不能用普通数组代替集合吗?@Oghenero-我以为你想让它快一点。字典是基于哈希表的,所以它有O(1)个查找时间。因此,查找重复项很快。要在数组中查找重复项,它必须扫描数组,这是一个O(n)操作。由于必须在循环内执行重复项检查,因此基于字典的完整解决方案为O(n),而基于数组的解决方案为O(n^2)—这是错误的。@Oghenero—字典是哈希表的实现。我认为“如何在VB6中实现哈希表?”将是它自己的问题。但我会帮你:我更新了我的答案,添加了最后一点。我不得不问,这是家庭作业吗?不,这不是家庭作业,但你为什么问这个?我更新了我的答案,添加了最后一点。我不得不问,这是家庭作业吗?不,这不是家庭作业,但你为什么问这个?