How to count the maximum consecutive set of numbers in an array with VB.NET

1 Answer

0 votes
Imports System
Imports System.Collections.Generic

Public Class Program
    Public Shared Function countMaxConsecutiveSetOfNumbers(ByVal arr As Integer()) As Integer
        Dim size As Integer = arr.Length
		Dim st As HashSet(Of Integer) = New HashSet(Of Integer)()

        For i As Integer = 0 To size - 1
			st.Add(arr(i))
        Next

        Dim mx As Integer = 0

        For i As Integer = 0 To size - 1
			If st.Contains(arr(i)) Then
				Dim temp As Integer = arr(i)

				While st.Contains(temp)
					temp += 1
				End While

				mx = Math.Max(mx, temp - arr(i))
			End If
        Next

        Return mx
    End Function

    Public Shared Sub Main(ByVal args As String())
        Dim arr As Integer() = New Integer() {22, 3, 21, 32, 24, 31, 4, 99, 23}
				
        Console.Write(countMaxConsecutiveSetOfNumbers(arr))
    End Sub
End Class

 



answered Oct 22, 2022 by avibootz
...