How to find the number occurring an odd number of times in an array with VB.NET

1 Answer

0 votes
Imports System

Public Class Program
    Public Shared Function get_number_that_occurring_an_odd_number_of_times_in_array(ByVal arr As Integer()) As Integer
        Dim value As Integer = 0
        Dim size As Integer = arr.Length

        For i As Integer = 0 To size - 1
            value = value Xor arr(i)
        Next

        For i As Integer = 0 To size - 1
            If arr(i) = value Then
                Return value
            End If
        Next

        Return -1
    End Function

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

        Console.Write(get_number_that_occurring_an_odd_number_of_times_in_array(arr))
    End Sub
End Class



' run:
'
' 3
'

 



answered Aug 31, 2022 by avibootz
edited Aug 18, 2024 by avibootz

Related questions

...