471,050 Members | 1,168 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,050 software developers and data experts.

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
2 2442
jg007
283 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
Killer42
8,435 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 | 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
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.