Check for duplicates in an array

By | 2018-01-06

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

Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.