VB.Net program to find occurrences of items in a one-dimensional array

Here, we are going to learn how to find occurrences of items in a one-dimensional array in VB.Net?
Submitted by Nidhi, on December 10, 2020

Here, we will create an array of integers and then read elements from the user. After that, we will find the occurrences of a given item in the array.

Program/Source Code:

The source code to find occurrences of items in a one-dimensional array is given below. The given program is compiled and executed successfully.

'VB.Net program to find occurrences of item 
'in a one-dimensional array.

Module Module1

    Sub Main()
        Dim arr As Integer() = New Integer(6) {}
        Dim item As Integer = 0
        Dim count As Integer = 0

        Console.WriteLine("Enter array elements: ")
        For i = 0 To 5 Step 1
            Console.Write("Element[{0}]: ", i)
            arr(i) = Integer.Parse(Console.ReadLine())
        Next

        Console.WriteLine("Enter Item: ")
        item = Integer.Parse(Console.ReadLine())

        For i = 0 To 5 Step 1
            If item = arr(i) Then
                count = count + 1
            End If
        Next
        Console.WriteLine("Total occurrences of {0} is:{1}", item, count)
    End Sub

End Module

Output:

Enter array elements:
Element[0]: 10
Element[1]: 12
Element[2]: 12
Element[3]: 14
Element[4]: 13
Element[5]: 12
Enter Item:
12
Total occurrences of 12 is:3
Press any key to continue . . .

Explanation:

In the above program, we created a module Module1 that contains a function Main(). In the Main() we created an array arr of six elements and we also created more variables item, count which are initialized with 0.

Console.WriteLine("Enter array elements: ")
For i = 0 To 5 Step 1
    Console.Write("Element[{0}]: ", i)
    arr(i) = Integer.Parse(Console.ReadLine())
Next

Console.WriteLine("Enter Item: ")
item = Integer.Parse(Console.ReadLine())

In the above code, we read the values of array elements and also read the value for the item.

For i = 0 To 5 Step 1
    If item = arr(i) Then
        count = count + 1
    End If
Next
Console.WriteLine("Total occurrences of {0} is:{1}", item, count)

In the above code, we find the occurrences of a given item and print the count on the console screen.

VB.Net Array Programs »





Comments and Discussions




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.