HS Banner
Back
Find missing numbers in a sequence.

Author: Admin 03/29/2021
Language: Visual Basic .NET
Tags:


Description:

Returns the missing numbers in a sequence.

Article:

''' Returns the missing numbers in a sequence.   
    ''' Expects a string of comma-delimited numbers. ex: "1,2,4,6,7" 
    Dim MissingNumbers As New StringCollection
    Public Function FindMissingNumbers(ByVal strNumbers As String) As StringCollection
        On Error Resume Next
        Dim arNumbers() As String
        arNumbers = Split(strNumbers, ",")
        arNumbers = SortNumbers(arNumbers)
        For I = 0 To UBound(arNumbers) - 1
            If CLng(arNumbers(I)) + 1 <> CLng(arNumbers(I + 1)) Then
                For J = 1 To (CLng(arNumbers(I + 1)) - CLng(arNumbers(I))) - 1
                    MissingNumbers.Add(CStr(CLng(arNumbers(I)) + J))
                Next
            End If
        Next
        Return MissingNumbers
    End Function

    ''' Sorts the array of numbers in value order, least to greatest.  
    ''' Send in a string() array of numbers
    Private Function SortNumbers(ByVal arNumbers() As String) As String()
        On Error Resume Next
        Dim tmpNumber As String
        For J = 0 To UBound(arNumbers) - 1
            For I = J + 1 To UBound(arNumbers)
                If arNumbers(I) - arNumbers(J) < 0 Then
                    tmpNumber = arNumbers(J)
                    arNumbers(J) = arNumbers(J)
                    arNumbers(I) = tmpNumber
                End If
            Next
        Next
        Return arNumbers
    End Function



Back
Comments
Add Comment
There are no comments yet.