Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class MostFrequentNumber
- {
- static void Main()
- {
- /*09.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)*/
- Console.Write("Array size: ");
- int arraySize = int.Parse(Console.ReadLine());
- int[] arr = new int[arraySize];
- int number;
- for (int index = 0; index < arraySize; index++)
- {
- Console.Write("Position {0} : ", index + 1);
- number = int.Parse(Console.ReadLine());
- arr[index] = number;
- }
- int[] arr2 = new int[arraySize];
- int count=0,maxCount=0;
- for (int i = 0; i < arr.Length; i++)
- {
- count = 0;
- for (int k = 0; k < arr.Length; k++)
- {
- if (arr[i] == arr[k])
- {
- count++;
- arr2[i] +=1 ;
- }
- if (count > maxCount)
- {
- maxCount = count;
- }
- }
- }
- for (int i = 0; i < arr2.Length; i++)
- {
- if (arr2[i] == maxCount)
- {
- Console.WriteLine("{0} ({1} times)", arr[i], maxCount);
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement