Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
MostFrequent
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { // mostFrequent(array1) should return 1. int[] array1 = {1, 3, 1, 3, 2, 1}; // mostFrequent(array2) should return 3. int[] array2 = {3, 3, 1, 3, 2, 1}; // mostFrequent(array3) should return null. int[] array3 = {}; // mostFrequent(array4) should return 0. int[] array4 = {0}; // mostFrequent(array5) should return -1. int[] array5 = {0, -1, 10, 10, -1, 10, -1, -1, -1, 1}; Console.WriteLine("Value is: "+mostFrequent(array3)); } public static int? mostFrequent(int[] array){ var numDict = new Dictionary<int, int>(); int max_count = -1; int? max_item = null ; for (int i=0; i< array.Length ; i++) { if( numDict.ContainsKey(array[i]) ) numDict[array[i]] +=1 ; else numDict[array[i]] = 1; if(numDict[array[i]] > max_count ) { max_count = numDict[array[i]]; max_item = array[i]; } } return max_item; } } }
run
|
edit
|
history
|
help
0
Fórum Parallel Threads ( Without Fun )
DN3
Classes, properties, methods, ling begining
Compare list
Single Line Date in Months
Animal generator (Under construction!)
HelloWorld
Test
1
числата от 1 до 20