How to get the minimum elements to be deleted to make an array with same values in VB.NET

1 Answer

0 votes
Imports System
Imports System.Collections.Generic

Public Class Example
    Public Shared Function minElementsToBeDelete(ByVal arr As Integer()) As Integer
        Dim frequency As Dictionary(Of Integer, Integer) = New Dictionary(Of Integer, Integer)()
        Dim size As Integer = arr.Length

        For i As Integer = 0 To size - 1
            If frequency.ContainsKey(arr(i)) Then
                frequency(arr(i)) = frequency(arr(i)) + 1
            Else
                frequency(arr(i)) = 1
            End If
        Next

        Dim max_frequency As Integer = Integer.MinValue
        For Each i As Integer In frequency.Keys
            max_frequency = Math.Max(max_frequency, frequency(i))
        Next

        Return size - max_frequency
    End Function

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

        Console.WriteLine(minElementsToBeDelete(arr))
    End Sub
End Class






' run
'
' 5
'

 



answered Dec 12, 2022 by avibootz
...