VBA to array weight: weight by various methods to the dictionary array deduplication method + two methods

 

A method of using a dictionary to heavy

  • The method, to use a dictionary weight
  • dict1(I) = ""
  • Method 2, use a dictionary to count the number of heavy +
  • dict2(I) = dict2(I) + 1
  • Method 3, with repeated dictionary newspaper, but did not go to repeat
  •  If Not dict3.exists(I) Then

 

Sub test_dict1()
Dim dict1 As Object
Set dict1 = CreateObject("scripting.dictionary")

Dim dict2 As Object
Set dict2 = CreateObject("scripting.dictionary")

Dim dict3 As Object
Set dict3 = CreateObject("scripting.dictionary")


arr1 = Range("b1:b10")

'去重
For Each I In arr1
   dict1(I) = ""
Next

For Each J In dict1.keys()
   Debug.Print J
Next
Debug.Print


'去重 + 统计次数
For Each I In arr1
   dict2(I) = dict2(I) + 1
Next

For Each J In dict2.keys()
   Debug.Print J
Next
Debug.Print

For Each K In dict2.items()
   Debug.Print K
Next
Debug.Print



For Each I In arr1
    If Not dict3.exists(I) Then
       dict3.Add I, ""
    Else
       Debug.Print "存在重复值"
       Exit Sub
    End If
Next
End Sub

 

2 array deduplication method

2.1 an array cycle may be repeated to array

  • Double loop
  • Key 1: The purpose of the two-cycle, the cycle take an array, and all of a number of additional cycles do comparison
  • 2 key points: the outer layer assignment
  • Key Point 3: The count variable assignment was independent, because they do not know there are several non-repeating number
Sub test_arr1()
Dim arr2

arr1 = Array(1, 2, 3, 4, 5, 1, 2, 3, 1, 2, 1)
ReDim arr2(UBound(arr1))    '在外面需要1次redim到位

For i = LBound(arr1) To UBound(arr1)
   For j = LBound(arr2) To UBound(arr2)

      If arr1(i) = arr2(j) Then
         GoTo line1
      End If
   Next
   arr2(k) = arr1(i)
   k = k + 1
line1: Next

For Each i In arr2
   Debug.Print i
Next

End Sub

 

2.2 circulating array method, each time determining the inner loop, if each time a complete cycle can take

  • Judge each inner loop, each time whether you can take a full cycle
  • In order to get back with a selective stop on the index ubound + 1, or are parked on ubound + 1 can not distinguish
  • The array index pointer over ubound + 1 proves to finish complete the internal circulation is not exit for, no proof is repeated
Sub test_arr2()
Dim arr2

arr1 = Array(1, 2, 3, 4, 5, 1, 2, 3, 1)
ReDim arr2(LBound(arr1) To UBound(arr1))  '这样也没问题,一般有重复的列肯定更长
'ReDim arr2(LBound(arr1) To 99999)  这样也可以,就是故意搞1个极大数

K = 0
For I = LBound(arr1) To UBound(arr1)
    For J = LBound(arr2) To K
        If arr1(I) = arr2(J) Then
           Exit For         '为了配合后面得index选择性的停在ubound+1上,否则都停在ubound+1上没法区分
        End If
    Next
    If J = K + 1 Then       'array的index指针停在ubound+1就证明内部循环完整走完没有exit for,证明无重复
       arr2(K) = arr1(I)
       K = K + 1
    End If
Next

Debug.Print
For Each m In arr2
   Debug.Print m;
Next
Debug.Print

End Sub

 

2.3 The method of using the array to check the number of repetitions

2.3.1 using an array of methods to check the number of repetitions of a target value

Sub test001()
'查某个值得重复次数

arr1 = Array(1, 2, 3, 4, 5, 1, 2, 3, 6, 7, 8, 9)
target1 = 1

Debug.Print "arr1的最小index=" & LBound(arr1)
Debug.Print "arr1的最大index=" & UBound(arr1)

For I = LBound(arr1) To UBound(arr1)
    If arr1(I) = target1 Then
       Debug.Print target1 & "第" & m & "个" & "index=" & I
    End If
Next


End Sub

 

 

2.3.2 The method of using the array to search the dictionary + the number of repetitions of each element

Check the number of all the elements


Sub test002()
'如果用循环方法查每个重复的值的重复次数
 
arr1 = Array(1, 2, 3, 4, 5, 1, 2, 3, 6, 7, 8, 9)
Dim dict1 As Object
Set dict1 = CreateObject("scripting.dictionary")

Debug.Print "arr1的最小index=" & LBound(arr1)
Debug.Print "arr1的最大index=" & UBound(arr1)
arr2 = arr1


For I = LBound(arr1) To UBound(arr1)
    m = 1
    For J = LBound(arr2) To UBound(arr2)
        If arr1(I) = arr2(J) Then
           dict1(arr1(I)) = m
           m = m + 1
        End If
    Next
Next


For Each I In dict1.keys()
    Debug.Print I & "," & dict1(I)
Next



End Sub

 

 

Only part of repetitions check element

Sub test002()
'如果用循环方法查每个重复的值的重复次数
 
arr1 = Array(1, 2, 3, 4, 5, 1, 2, 3, 6, 7, 8, 9)
Dim dict1 As Object
Set dict1 = CreateObject("scripting.dictionary")

Debug.Print "arr1的最小index=" & LBound(arr1)
Debug.Print "arr1的最大index=" & UBound(arr1)
arr2 = arr1


For I = LBound(arr1) To UBound(arr1)
    m = 1
    For J = LBound(arr2) To UBound(arr2)
        If arr1(I) = arr2(J) Then
              If m >= 2 Then
                 dict1(arr1(I)) = m
              End If
           m = m + 1
        End If
    Next
Next


For Each I In dict1.keys()
    Debug.Print I & "," & dict1(I)
Next



End Sub

 

 

2.3.3 search with an array of methods pure it?

  • not good enough
  • Because still have to go heavy, otherwise it will display the results as very stupid

Sub test002()
'如果用循环方法查每个重复的值的重复次数
 
arr1 = Array(1, 2, 3, 4, 5, 1, 2, 3, 6, 7, 8, 9)

Debug.Print "arr1的最小index=" & LBound(arr1)
Debug.Print "arr1的最大index=" & UBound(arr1)
arr2 = arr1   '每个元素必然至少重复1次


For I = LBound(arr1) To UBound(arr1)
    m = 0
    For J = LBound(arr2) To UBound(arr2)
        If arr1(I) = arr2(J) Then
           m = m + 1
        End If
    Next
    Debug.Print arr1(I) & "重复了" & m & "次"
Next


End Sub

 

 

 

Other methods

I did not understand

https://cloud.tencent.com/developer/article/1468729

With the collection method

https://www.cnblogs.com/sylar-liang/p/5563610.html

https://mbd.baidu.com/newspage/data/landingsuper?context={%22nid%22%3A%22news_9430753016572783149%22}&n_type=1&p_from=3

发布了370 篇原创文章 · 获赞 45 · 访问量 9万+

Guess you like

Origin blog.csdn.net/xuemanqianshan/article/details/103899067