0

I need to remove reverse duplicates from the array.

From 1,2 and 2,1 I want to remove 2,1 since it's reverse is a duplicate for 1,2.

Sub go()
    Dim arr() As String
    ReDim Preserve arr(0)

    Dim i, k As Integer

    For i = 1 To 2
        For k = 1 To 10    
            If i <> k Then
                arr(UBound(arr)) = i & "," & k
                ReDim Preserve arr(UBound(arr) + 1)
            End If
        Next
    Next
    'Debug.Print Join(arr, vbCrLf)

    For z = 0 To UBound(arr)
        For q = 0 To UBound(arr)    
            rev = StrReverse(arr(z))
            If arr(q) = rev Then
                Debug.Print arr(z) & "-" & rev
            End If
        Next
    Next
End Sub
6
  • Can you explain what your question is, or how this doesn't work? Commented Oct 28, 2019 at 12:17
  • I do not know how to remove the 2,1 duplicate from the arr() array. Commented Oct 28, 2019 at 12:20
  • 1
    maybe just copy it to a new array? you don't want to change the array you are iterating over in yourloop. Commented Oct 28, 2019 at 12:23
  • 1
    Adding to @Jeremy comment, you could have a loop to go through your array. Within the loop have a collection. Check if your current item exists in collection, if it doesn't, add to the collection and your new array Commented Oct 28, 2019 at 12:40
  • Unrelated, but the following line most likely doesn't do what you think it's doing: Dim i, k As Integer. What you actually have declared here is Dim i As Variant, k As Integer. VB6 & VBA don't support declaring multiple variables as a certain data type in one statement. If you want to declare both as Integer, the declaration needs to look like Dim i As Integer, k As Integer. Commented Oct 28, 2019 at 13:33

1 Answer 1

1

As the comments said, the easiest way to do this is a have a new array and add elements to it. Also as Hel O'Ween said, VBA variables need explicit declarations like I have below.

Public Sub goTest()

Dim inputArray() As String
ReDim Preserve inputArray(0)
Dim resultArray() As String
ReDim Preserve resultArray(0)

Dim i As Long, j As Long
Dim rev As String, notDuped As Boolean

'[[-- Loop to generate the test array
For i = 1 To 2
    For j = 1 To 10
        If i <> j Then
            inputArray(UBound(inputArray)) = i & "," & j
            ReDim Preserve inputArray(UBound(inputArray) + 1)
        End If
    Next j
Next i
'--]]

'The actual reverse removed code
For i = 0 To UBound(inputArray)
    rev = StrReverse(inputArray(i))
    notDuped = True
    For j = 0 To i 'Only loop up to the current point, not all of the array
        If inputArray(j) = rev Then
            notDuped = False
            Exit For
        End If
    Next j
    If notDuped Then 'If no reverse found add to new array
        resultArray(UBound(resultArray)) = inputArray(i)
        ReDim Preserve resultArray(UBound(resultArray) + 1)
    End If
Next i

'[[-- To check that it worked
For i = 0 To UBound(resultArray)
    Debug.Print (resultArray(i))
Next i
'--]]

End Sub

The bottom section of code just outputs the resulting array so you can see it has no reverses, it has no bearing on the code working correctly.

If you have any problems with this just leave me a comment and I'll get back to you.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, it did the job. I only had to change 'The actual reverse removed code For i = 0 To UBound(inputArray) rev = StrReverse(inputArray(i)).
No problem J Doe. Can you tell me why the i needed changed to 0 instead of 1?
Because setting i = 1 will remove both 1,2 and 2,1. But I wanted to keep 1 of the duplicates, so setting i = 0 keeps 1,2 and removes 2,1. Probably I did not made it clear what I needed. Sorry for that.
No, you explained it fine, It's more a case of me forgetting that skipping the first element because it can't have a duplicate also skips adding it to the results.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.