Siyabonga Kubeka
Siyabonga Kubeka

Reputation: 43

How do I indicate the position of the occurrence of a number in an array VB.Net

Search Button: This button should determine how many times did the number specified in a text box was entered. Additional indicate the position of the occurrence

This is how I have written my code but it does not show the position of the occurrence in an array:

Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
    Dim count As Integer = 0
    Dim value As Integer = txbValues.Text()
    For Each num As Integer In numbers
        If num = value Then count = count + 1
    Next
    txbOccurances.Text = count

    Dim index0 As Integer = Array.IndexOf(numbers, value)
    Dim index1 As Integer = Array.LastIndexOf(numbers, value)


    txbPosition.Text = index0 & " " & index1
End Sub

The array that I am searching in is numbers(4). The program should be something like as shown below:

enter image description here

Upvotes: 0

Views: 729

Answers (3)

jmcilhinney
jmcilhinney

Reputation: 54417

The Array.IndexOf method let's you specify the index from which to start searching so, each time you find an occurrence, you can start searching again from the next index:

Private Function GetIndexes(array As Integer(), value As Integer) As Integer()
    Dim indexes As New List(Of Integer)
    Dim startIndex = 0
    Dim index As Integer

    Do
        index = System.Array.IndexOf(array, value, startIndex)

        If index = -1 Then
            'No more occurrences found.
            Exit Do
        End If

        indexes.Add(index)

        startIndex = index + 1
    Loop Until startIndex = array.Length

    Return indexes.ToArray()
End Function

E.g.

Dim array = {0, 1, 2, 3, 1, 5, 6, 7, 1, 9}
Dim value = 1

Dim indexes = GetIndexes(array, value)

Console.WriteLine("Occurrences: " & indexes.Length)
Console.WriteLine("Indexes: " & String.Join(" ", indexes))

Upvotes: 1

Alessandro Mandelli
Alessandro Mandelli

Reputation: 581

My variation on ADyson's program:

    Dim numbers() As Integer = {1, 1, 4, 5, 2, 1} 'array to be searched
    Dim value As Integer = 1 'number to search for

    Dim index As New Dictionary(Of Integer, Integer)

    For Each num As Integer In numbers
        If Not index.ContainsKey(num) Then
            index.Add(num, 1)
        Else
            index(num) += 1
        End If
    Next

    Console.WriteLine("Occurences: " & index(value))
    Console.Write("Indexes: ")

    For i = 0 To numbers.Count - 1
        If numbers(i) = value Then
            Console.Write(i & " ")
        End If
    Next

Upvotes: 0

ADyson
ADyson

Reputation: 61904

Your current code actually works for the example you've given. The problem though is it will only work if there are two or less occurrences of the number in the array. If you get beyond that it will only find the first and last occurrence.

Instead you can maintain a list of all the indexes you've found which contain a matching number - you can do this during the existing loop you make to test the values.

Here is sample code which will run in a console application, to demonstrate the idea...you can replace the "numbers" array and "value" integer with your real inputs from the user, and replace the Console writes with code to populate your textboxes.

Dim numbers() as Integer = {1, 1, 4, 5, 2, 1} 'array to be searched
Dim value As Integer = 1 'number to search for

Dim index = 0
Dim indexes As New List(Of Integer)

For Each num As Integer In numbers
    If num = value Then 
        indexes.Add(index)
    End If
    index += 1
Next

Console.WriteLine("Occurences: " & indexes.Count)
Console.Write("Indexes: ")

For Each ind as Integer In indexes
    Console.Write(ind & " ")
Next

This will output:

Occurences: 3
Indexes: 0 1 5

Upvotes: 1

Related Questions