They should therefore be treated somewhat differently too.
Because it should work correctly independent of "casing" (lower-/upper-/mixed-case),
we explicitly compare equally cased strings.
Below code is explicitly string specific and caters for two scenarios:
- default: mixed lengths of strings (in array), mostly expected.
- otherwise: equal lengths of strings expected/known (depends on input, e.g. *.csv file)
(NO chronological order of the elements attempted.):
Code: Select all
Sub MakeUnique( _ ' case insensitive check
a() As String, _
ByVal chlen As Boolean = TRUE _ ' default: do len check first!
)
Var i = LBound(a), j = 0, _
k = UBound(a)
If chlen Then ' faster if: mixed string length
While i < k ' only if both string lengths are equal _
j = i + 1 ' we do a full string comparison, other- _
While j <= k ' wise: quit early
If Len(a(i)) = Len(a(j)) Then
If LCase(a(i)) = LCase(a(j)) Then
a(j) = a(k)
k -= 1
Else
j += 1
End If
Else
j += 1
End If
Wend
i += 1
Wend
Else ' faster if: strings are mostly/always _
While i < k ' of the same length
j = i + 1
While j <= k
If LCase(a(i)) = LCase(a(j)) Then
a(j) = a(k)
k -= 1
Else
j += 1
End If
Wend
i += 1
Wend
End If
Redim Preserve a(LBound(a) To k) ' remove 'empties'
End Sub