Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class MostFreqNumber
- {
- static void Main()
- {
- Console.WriteLine("Please, Insert the length of the array!");
- int n = int.Parse(Console.ReadLine());
- // Read the array
- int[] array = new int[n];
- for (int i = 0; i < n; i++)
- {
- array[i] = int.Parse(Console.ReadLine());
- }
- // Example: {4, 1, 1, 4, 2, 3, 4, 4, 1, 2, 4, 9, 3} 4 (5 times)
- // Check the most frequent number
- // Using Selection Sort
- int currentMin;
- int tempValue;
- for (int i = 0; i < array.Length - 1; i++)
- {
- currentMin = i;
- for (int j = i + 1; j < array.Length; j++)
- {
- if (array[j] < array[currentMin])
- {
- currentMin = j;
- }
- }
- tempValue = array[i];
- array[i] = array[currentMin];
- array[currentMin] = tempValue;
- }
- // Count and Compair the lengths
- int count = 0;
- int getValue = 0;
- int prnLength = 0;
- for (int i = 0; i < array.Length - 1; i++)
- {
- if (array[i] == array[i + 1])
- {
- count++;
- }
- else
- {
- count = 0;
- }
- if (count > prnLength)
- {
- prnLength = count;
- getValue = array[i];
- }
- }
- // Print
- Console.WriteLine("{0} ({1} times)", getValue, prnLength + 1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement