Home »
VB.Net »
VB.Net Programs
VB.Net program to find occurrences of items in a one-dimensional array
By Nidhi Last Updated : November 14, 2024
Find occurrences of items in an array
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 code to find occurrences of items in an array
'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 »