Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,926 questions

51,859 answers

573 users

How to check if an array contain consecutive integers in VB.NET

1 Answer

0 votes
Imports System
Imports System.Linq
Imports System.Collections.Generic

Public Class AClass
    Public Shared Function array_contain_consecutive_integers(ByVal arr As Integer()) As Boolean
        If arr.Length <= 1 Then
            Return True
        End If

        Dim min As Integer = arr.Min()
        Dim max As Integer = arr.Max()

        If max - min <> arr.Length - 1 Then
            Return False
        End If

        Dim st As ISet(Of Integer) = New HashSet(Of Integer)()

        For Each val As Integer In arr
            If st.Contains(val) Then
                Return False
            End If

            st.Add(val)
        Next

        Return True
    End Function

    Public Shared Sub Main(ByVal args As String())
        Dim arr As Integer() = New Integer() {-2, 3, 0, -1, 4, 2, 1}

        If array_contain_consecutive_integers(arr) Then
            Console.Write("Yes")
        Else
            Console.Write("No")
        End If
    End Sub
End Class




' run:
'
' Yes
'

 



answered Aug 19, 2022 by avibootz

Related questions

1 answer 111 views
1 answer 95 views
1 answer 103 views
2 answers 134 views
1 answer 91 views
...