need one little piece of code to allow me to find duplicate arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • raphael001
    New Member
    • Mar 2008
    • 1

    need one little piece of code to allow me to find duplicate arrays

    In my Visual Basic program I'm just trying to find duplicate values entered into an array from an inputbox, but i can't seem to get the coding right on the final part to check for duplicate values already entered. Any help would be greatly appreciated. This is what i have so far except the code to check for duplicate values:

    [CODE=vb]Dim intarray() As Integer
    Dim intindex As Integer
    Dim intcount As Integer
    Dim intsearch As Integer
    Dim intlow As Integer, inthigh As Integer
    intlow = -32676
    inthigh = 32676
    ReDim intarray(intlow To inthigh)

    For intindex = LBound(intarray ) To UBound(intarray )
    Do
    intnumber = InputBox("Enter a number:", "Enter number")
    If intnumber < 1 Or intnumber > 99 Then
    MsgBox "Please enter numbers only between 1 and 99"
    End If
    intarray(intind ex) = intnumber
    intcount = intcount + 1
    lstnums.AddItem intnumber
    Loop Until 'duplicate is found
    Next intindex
    lbldisplay.Capt ion = "Duplicate number after " & intcount & " times."

    End Sub[/CODE]
    Last edited by debasisdas; Mar 31 '08, 06:04 AM. Reason: added code=vb tags
  • jg007
    Contributor
    • Mar 2008
    • 283

    #2
    Originally posted by raphael001
    In my Visual Basic program I'm just trying to find duplicate values entered into an array from an inputbox, but i can't seem to get the coding right on the final part to check for duplicate values already entered. Any help would be greatly appreciated.
    what about doing a bubble / shell sort then running throught the values comparing them to the next one in the array, the only problem would be if you did not want the values sorted

    here is a link for shell sort which can be ammended to sort text instead of numbers although for some reason it only seems to work once the array is at least 5 long

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      The sort seems a bit over the top, since it will have to comapre all the values at least once anyway.

      Just do a nested loop. For example...

      [CODE=vb]Dim I As Long, J As Long
      For I = 1 To SizeOfArray - 1
      For J = I + 1 To SizeOfArray
      If Array(I) = Array(J) Then
      DuplicateFlag = True
      Exit For
      End If
      Next
      If DuplicateFlag Then Exit For
      Next
      [/CODE]This is just off the top of my head, don't take it as Gospel. :)

      Comment

      Working...