Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,907 questions

51,839 answers

573 users

How to find elements that appear more than array_size/K times in an array with VB.NET

1 Answer

0 votes
Imports System
Imports System.Collections.Generic

Public Class Program
    Public Shared Function elements_that_appear_more_than_x_times(ByVal arr As Integer(), ByVal k As Integer) As Dictionary(Of Integer, Integer)
        Dim size As Integer = arr.Length
        
		Dim times As Integer = size / k
        Console.WriteLine("more than " & times & " times")
        
		Dim freqency_map As Dictionary(Of Integer, Integer) = New Dictionary(Of Integer, Integer)()

        For i As Integer = 0 To size - 1
            If freqency_map.ContainsKey(arr(i)) <> False Then
                freqency_map(arr(i)) += 1
            Else
                freqency_map.Add(arr(i), 1)
            End If
        Next

        Return New Dictionary(Of Integer, Integer)(freqency_map)
    End Function

    Public Shared Sub Main(ByVal args As String())
        Dim k As Integer = 4
        Dim arr As Integer() = New Integer() {4, 8, 6, 5, 5, 8, 3, 2, 1, 2, 2, 5, 5, 5, 5, 8, 9, 8, 8}
        
		Dim freqency_map As Dictionary(Of Integer, Integer) = elements_that_appear_more_than_x_times(arr, k)

        For Each item As KeyValuePair(Of Integer, Integer) In freqency_map
            If item.Value > arr.Length / k Then
                Console.WriteLine("{0} ", item.Key)
            End If
        Next
    End Sub
End Class



' run:
'
' more than 5 times
' 8 
' 5
'

 



answered Feb 10, 2024 by avibootz
...