Check for duplicates in an array

Posted On 2018-01-06 by ArthurChaparyan
Keywords:
Tags: VB6 String Handling 
Views: 50


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.

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

Special Instructions

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


About the Author

ArthurChaparyan has posted a total of 1 articles.


Comments On This Post

No comments on this post yet!


Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.


Your IP address is:54.82.57.154

Before you can post, you need to prove you are human. If you log in, this test goes away.




Code Links