Visual Basic - Finding Array Duplicate Help

Discussion in 'Programming' started by NsaneNoob, Mar 16, 2008.

  1. #1
    I have this one array and what I am trying to do is find after how many numbers is there a duplicate number. So if a list is:

    5
    4
    6
    3
    5

    Then it should display that the duplicate happens after 4 numbers.

    Here's my code:


    Option Explicit
    
    
    Dim ind As Integer
    Dim arrays() As Integer
    Dim size As Integer
    Dim i As Integer
    
    
    
    Private Sub Form_Load()
    Randomize
    size = 5
    
    ReDim arrays(size)
    For ind = LBound(arrays) To UBound(arrays)
        arrays(ind) = Int(size * Rnd + 1)
            
    Next ind
    
    
    For ind = LBound(arrays) To UBound(arrays)
        List1.AddItem arrays(ind)
    Next ind
    Code (markup):
     
    NsaneNoob, Mar 16, 2008 IP
  2. vbrocks

    vbrocks Active Member

    Messages:
    90
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    98
    #2
    Would this be what you need?

    (add a button and paste the code)
    
    Private Sub Command1_Click()
        Dim i As Long
        Dim k As Long
        Dim itemvalue As String
        For i = 0 To List1.ListCount - 1
            itemvalue = List1.List(i)
            For k = i + 1 To List1.ListCount - 1
                If List1.List(k) = itemvalue Then
                    MsgBox "Duplicate of item#" & CStr(i) & " found after " & CStr(k - i) & " items"
                    Exit For
                End If
            Next k
        Next i
    End Sub
    
    Code (markup):
     
    vbrocks, Mar 20, 2008 IP
    NsaneNoob likes this.