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 C#

1 Answer

0 votes
using System;
using System.Collections.Generic;

public class Program
{
	public static Dictionary<int, int> elements_that_appear_more_than_x_times(int[] arr, int k) {
		int size = arr.Length;
		int times = size / k;
		Console.WriteLine("more than " + times + " times");

		Dictionary<int, int> freqency_map = new Dictionary<int, int>();

		for (int i = 0; i < size; i++) {
			if (freqency_map.ContainsKey(arr[i]) != false) {
				freqency_map[arr[i]]++;
			} else {
				freqency_map.Add(arr[i], 1);
			}
		}

		return new Dictionary<int, int>(freqency_map);
	}

	public static void Main(string[] args)
	{
		int k = 4;
		int[] arr = new int[] {4, 8, 6, 5, 5, 8, 3, 2, 1, 2, 2, 5, 5, 5, 5, 8, 9, 8, 8};

		Dictionary<int, int> freqency_map = elements_that_appear_more_than_x_times(arr, k);
        
		foreach (KeyValuePair<int, int> item in freqency_map) { 
		        if (item.Value > arr.Length / k) {
                    Console.WriteLine("{0} ", item.Key); 
		        }
        }
	}
}



/*
run:

more than 4 times
8 
5

*/

 



answered Feb 10, 2024 by avibootz
...