English 中文(简体)
2. 消除重复
原标题:remove duplicates faster vb6
  • 时间:2011-02-03 17:24:15
  •  标签:
  • vb6

i 本功能在去除第6条中的重复

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

I need to optimize this function to run faster, please help

最佳回答
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
问题回答

利用强调(而不是大量)和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




相关问题
Prevent windows from queuing shellexecute requests

Win.ShellExecute 0, "open", "C:dirprogram.exe", "arguments", vbNullString, SW_SHOWNORMAL Win.ShellExecute 0, "open", "http://www.google.com", vbNullString, vbNullString, SW_SHOWNORMAL I want google....

Why is My Loop Only Deleting One File?

Using VB6 In a folder, i have n number of files, i want to delete a 0 kb files code Dim filename5 As String filename5 = Dir$(txtsourcedatabasefile & "*_*", vbDirectory) MsgBox filename5 Do ...

How to check the filesize?

Using VB6 I have the text file with different sizes, so i want to delete the file where filesize = 0 kb. How to make a vb6 code for deleting the 0 kb files. Need vb6 code Help

File Rename problem?

I m using VB6 and I have a folder where I have n number of files. I want to change the file extension to .txt. I used the code below to change the extension of all .fin files to .txt. Dim filename1 ...

Error 20728-F while in using Crystal Reports in VB6

I m using Crystal Reports in my VB6 project, but I m facing error while loading the report in crystalreport1.action=1; Please give me some solution for this problem. It is showing the error as Error ...

DllRegisterServer entry point was not found

When running my vb6 application I am getting error like, runtime error 53 : file not found: rscomclNoMsg.dll then i tried to register that dll from cmd line using regsvr32. Then I am getting ...

SQL Server 2000, ADO 2.8, VB6

How to determine if a Transaction is active i.e. before issuing Begin Transaction I want to ensure that no previous transaction are open.. the platform is VB6, MS-SQL Server 2000 and ADO 2.8

热门标签