Important alert: (current site time 7/16/2013 1:54:02 AM EDT)
 

VB icon

(Updated Again) Check for duplicates in an array!

Email
Submitted on: 8/7/1999
By: Arthur Chaparyan3  
Level: Not Given
User Rating: By 8 Users
Compatibility: VB 4.0 (16-bit), VB 4.0 (32-bit), VB 5.0, VB 6.0
Views: 45602
 
     I've been looking around for this code and no one could provide it. So finally I wrote it. It checks for duplicates in an array and returns true if there are any.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: (Updated Again) Check for duplicates in an array!
' Description:I've been looking around for this code and no one could provide it. So finally I wrote it. It checks for duplicates in an array and returns true if there are any.
' By: Arthur Chaparyan3
'
' Inputs:The only input required is the array
'
' Returns:Returns True if there are any duplicates and false otherwise
'
' Assumes:You could use this code to generate lottery numbers or check if more than one record of the same name is present...
'
' Side Effects:If used with LARGE (and I mean LARGE as in arrays with hundreds or thousands of items) it will slow down. USE WITH ONE DIMENSIONAL ARRAYS
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=2856&lngWId=1'for details.'**************************************

Public Function AnyDup(NumList As Variant) As Boolean
 Dim a As Long, b As Long
 'Start the first loop
 For a = LBound(NumList) To UBound(NumList)
 'Start the second loop (thanks for the suggestions everyone)
 For b = a + 1 To UBound(NumList)
 'Check if the values are the same
 'if they're equal, then we found a duplicate
 'tell the user and end the function
 If NumList(a) = NumList(b) Then AnyDup = True: Exit Function
 Next
 Next
End Function


Other 3 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Not Given category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

8/8/1999 9:51:00 AMMaxwell

I like it. Nice and simple thats the way all code should be. Good job.
(If this comment was disrespectful, please report it.)

 
8/8/1999 9:19:00 PMnormn

have you tried setting the lower bound of
the second for loop to "a". since the first
item is already checked through the list you
don't have to keep checking it the further down
the list you go. it should speed up the function
(well just a suggestion don't know if it will work
but something i was just thinking about)
(If this comment was disrespectful, please report it.)

 
8/9/1999 6:32:00 AMVincent

Normn is right. then second loop should be:
For b = a To UBound(NumList)


(If this comment was disrespectful, please report it.)

 
8/10/1999 6:38:00 AMVincent

I've been thinking to this little problem and come with the fastest solution. One of the problem is that the LBound and Ubound are evaluated at each iteration of the loop. Then, the second loop can start at a+1 this way b will never be equal to a and we can remove the if a <> b condition.

This could look like

Dim Lb as long
Dim Ub as long

Lb = LBound(NumList)
Ub = UBound(NumList)

for a = Lb to Ub -1 'Minus one to
'avoid a + 1 > Ub
'in next loop
for b = a + 1 to Ub
If NumList(a) = NumList(b) Then AnyDup = True: Exit Function
next b
next a

'Please note that the use of Variant data type also slow down the processing.
'Also and very important: Ubound and LBound return a long data type !!! a huga array will crash if you Dim a and b as integers.
'I may be forgotting a few other things but I think it will help.
(If this comment was disrespectful, please report it.)

 
6/1/2000 12:40:30 PMJeffrey C. Tatum

I didn't try your code, but I'm sure it works. There is a faster way though, and I'm not sure its what people want to use, but it is fast. It's basically as fast as loading a list, has no lag or loops what so ever. All you do is make the list sorted, then add an index to a list. If the NewIndex - 1 = NewIndex, then its a dupe. If you do a search for my name on this site, you will see two different versions of this code. Dupe Check with no lag =)
(If this comment was disrespectful, please report it.)

 
11/3/2000 9:01:05 PMStingRaY

I agree with Vincent's code
b should start from a+1
and to reduce the process, you should
assign UBound(NumList) to any variable.
This will reduce the overhead.
(If this comment was disrespectful, please report it.)

 
4/3/2001 3:10:19 PMEliminate

How to eliminate those duplicates from there ?
(If this comment was disrespectful, please report it.)

 
2/8/2002 11:07:43 AMbrye13

With large lists you lose a lot of functionality but checking the entire list twice. It will take at least the square of the number of items in the array to process the information.

If you sort the array first, then check for sequential duplicates you will save time.
(If this comment was disrespectful, please report it.)

 
2/11/2002 11:50:00 PMcichlid

Hey,
u may lose functionality, but if it's good enough for santa claus, its good enough for me.

(If this comment was disrespectful, please report it.)

 
1/3/2003 11:50:48 PMJadoo_Wala_Sadhoo

good job... just replace ubound and lbound with some variables. Fair enough to give good rating.
(If this comment was disrespectful, please report it.)

 
2/17/2003 8:18:22 AMJOSHUA STEWARD

but if it starts checking for duplicates at a + 1 what about before a. like further down in the list?
(If this comment was disrespectful, please report it.)

 
12/6/2003 8:16:07 AMTony G

Great code, I use it in a sorted multi-select listbox to remove dups, hope it works OK.
 For a = 0 To List1.ListCount - 1
  For b = a + 1 To List1.ListCount - 1
Result = StrComp(List1.List(a), List1.List(b))
      If Result = 0 Then
            List1.RemoveItem a
            a = a - 1
            End If
        Next b
    Next a
Duplicates should probably be an option in the listbox control but that's a different issue. Thanks :)
(If this comment was disrespectful, please report it.)

 
5/22/2004 3:06:50 AMAndrew Day

This code check exact duplicates only!!!
(If this comment was disrespectful, please report it.)

 
9/28/2004 9:30:09 AM

do you know how to use array in saving for database use? thanks
(If this comment was disrespectful, please report it.)

 
12/8/2006 4:02:30 AMFlintstone

You can do another thing is sort the array and while parsing in a loop check if the previous element is equal to current. If true, it has duplicates.
(If this comment was disrespectful, please report it.)

 
2/3/2007 3:22:54 PMwmanguiano

it may be some slow for check a too many elements array
(If this comment was disrespectful, please report it.)

 
12/29/2007 2:44:21 AMMiiro Bels

how do i apply it on a list box ie in the button that removes duplicates.
(If this comment was disrespectful, please report it.)

 
2/29/2008 5:07:34 AMralph

nice!...
(If this comment was disrespectful, please report it.)

 
2/29/2008 5:09:33 AMampao

try to give what contorl or how many label should i use????
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.