Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- // Write a program that finds the most frequent number in an array. Example:
- // {4, 1, 1, 4, 2, 3, 4, 4, 1, 2, 4, 9, 3} --> 4 (5 times)
- class MostFrequentNumber
- {
- static void Main()
- {
- //Input
- int[] array = { 03, -03, 0, 00, -0, -00 };
- int loop = (array.Length - 1);
- int counter = 1;
- int maxCounter = 0;
- int maxElement = 0;
- //Solution
- Array.Sort(array);
- for (int i = 0; i < loop; i++)
- {
- if (array[i] == array[i + 1])
- {
- counter++;
- if (counter > maxCounter)
- {
- maxCounter = counter;
- maxElement = array[i];
- }
- }
- else
- {
- counter = 1;
- }
- }
- //Output
- Console.WriteLine("{0} --> ({1} times)",maxElement,maxCounter);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement