Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Find the most frequent number in the array:
- Console.WriteLine("Enter length of the array: ");
- int length;
- //Length must be non-negative integer.
- do
- {
- Console.Write("Length = ");
- }
- while(!int.TryParse(Console.ReadLine(), out length) || length < 0);
- int[] array = new int[length];
- for(int i = 0; i < length; i++)
- {
- array[i] = Console.ReadLine();
- }
- int mostFrequent = 0;
- int maxCount = 0;
- for(int i = 0; i < array.Length; i++)
- {
- int counter = 0;// declaring the temp counter which will count the current occurrences of the number.
- for(int j = 0; j < array.Length; j++)
- {
- if(array[j] == array[i])
- {
- counter++;
- }
- }
- if(counter > maxCount)
- {
- maxCount = counter;
- mostFrequent = array[i];
- }
- }
- Console.WriteLine("Most frequent number in array: {0}\nRepeated {1}", mostFrequent, maxCount);
Advertisement
Add Comment
Please, Sign In to add comment