467,136 Members | 1,452 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,136 developers. It's quick & easy.

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:

Expand|Select|Wrap|Line Numbers
  1. Dim intarray() As Integer
  2. Dim intindex As Integer
  3. Dim intcount As Integer
  4. Dim intsearch As Integer
  5. Dim intlow As Integer, inthigh As Integer
  6. intlow = -32676
  7. inthigh = 32676
  8. ReDim intarray(intlow To inthigh)
  9.  
  10. For intindex = LBound(intarray) To UBound(intarray)
  11. Do
  12. intnumber = InputBox("Enter a number:", "Enter number")
  13. If intnumber < 1 Or intnumber > 99 Then
  14. MsgBox "Please enter numbers only between 1 and 99"
  15. End If
  16. intarray(intindex) = intnumber
  17. intcount = intcount + 1
  18. lstnums.AddItem intnumber
  19. Loop Until 'duplicate is found
  20. Next intindex
  21. lbldisplay.Caption = "Duplicate number after " & intcount & " times."
  22.  
  23. End Sub
Mar 31 '08 #1
  • viewed: 1848
Share:
2 Replies
100+
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

http://www.dreamincode.net/code/snippet857.htm
Mar 31 '08 #2
Expert 8TB
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...

Expand|Select|Wrap|Line Numbers
  1. Dim I As Long, J As Long
  2. For I = 1 To SizeOfArray - 1
  3.   For J = I + 1 To SizeOfArray
  4.     If Array(I) = Array(J) Then
  5.       DuplicateFlag = True
  6.       Exit For
  7.     End If
  8.   Next
  9.   If DuplicateFlag Then Exit For
  10. Next
  11.  
This is just off the top of my head, don't take it as Gospel. :)
Apr 1 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by lawrence | last post: by
41 posts views Thread by Psykarrd | last post: by
1 post views Thread by amerar@iwc.net | last post: by
21 posts views Thread by Johan Tibell | last post: by
reply views Thread by U S Contractors Offering Service A Non-profit | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.