-
Notifications
You must be signed in to change notification settings - Fork 2
/
basic_extras_ArrayUnique.bas
60 lines (57 loc) · 4.5 KB
/
basic_extras_ArrayUnique.bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
'--------------------------------------------------------------------------------------------------'
' ArrayUnique '
'--------------------------------------------------------------------------------------------------'
' Returns an array containing elements of input array without duplicate values. '
' '
' Parameters: '
' '
' Arr As Variant '
' The input array. '
' '
' Examples: '
'--------------------------------------------------------------------------------------------------'
' '
' result_array = ArrayUnique(input_array) '
' '
' Expected values: '
' '
' input_array: '
' Array(10,"vodka","10","beer","water",12,"beer","applejuice",12) '
' result_array: '
' Array(10,"vodka","10","beer","water",12,"applejuice") '
'--------------------------------------------------------------------------------------------------'
' '
' Sub TestArrayUnique '
' Dim item As Variant '
' Dim result As String '
' Dim inputarr As Variant '
' Dim resultarr As Variant '
' result = "" '
' inputarr = Array(10,"vodka","10","beer","water",12,"beer","applejuice",12) '
' resultarr = ArrayUnique(inputarr) '
' For Each item In resultarr '
' result = result + IIf(TypeName(item) = "String", """" + item + """", item) + "," '
' Next item '
' MsgBox(result) '
' End Sub '
' '
'--------------------------------------------------------------------------------------------------'
' Feedback & Issues: '
' https://github.com/aa6/libreoffice_calc_basic_extras/issues '
'--------------------------------------------------------------------------------------------------'
Function ArrayUnique(Arr As Variant)
Dim item As Variant
Dim entry As Variant
Dim result() As Variant
For Each item In Arr
For Each entry In result
If item = entry Then
Goto ArrayUniqueNextArrItem
End If
Next entry
Redim Preserve result(UBound(result) + 1) As Variant
result(UBound(result)) = item
ArrayUniqueNextArrItem:
Next item
ArrayUnique = result
End Function