How to count array elements with values in a given range with VB.NET

1 Answer

0 votes
Imports System

Public Class Program
    Public Shared Function countValuesInRange(ByVal arr As Integer(), ByVal from As Integer, ByVal to_ As Integer) As Integer
        Dim count As Integer = 0
        Dim size As Integer = arr.Length

        For i As Integer = 0 To size - 1
            If arr(i) >= from AndAlso arr(i) <= to_ Then count += 1
        Next

        Return count
    End Function

    Public Shared Sub Main()
        Dim arr As Integer() = {3, 6, 0, 17, 9, 1, 8, 7, 4, 10, 18, 11, 20, 30}
        Dim from As Integer = 5, to_ As Integer = 12
		
        Console.Write(countValuesInRange(arr, from, to_))
    End Sub
End Class




' run:
'
' 6
'

 



answered Dec 16, 2021 by avibootz

Related questions

...