Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class MostFrequentNumber
- {
- static void Main()
- {
- //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)
- int[] array = { 4, 1, 1, 4, 2, 3, 4, 4, 1, 2, 4, 1, 1, 1, 9, 3 };
- Console.WriteLine("{0}", string.Join(", ", array));
- Console.WriteLine();
- int maxSequence = 1;
- int tempSequence = 1;
- int placeholder = 0;
- for (int i = 0; i < array.Length; i++)
- {
- for (int j = 1; j < array.Length; j++)
- {
- if (array[i] == array[j])
- {
- tempSequence++;
- }
- }
- if (maxSequence < tempSequence)
- {
- maxSequence = tempSequence;
- placeholder = i;
- }
- tempSequence = 0;
- }
- Console.WriteLine("Number {0} is repeated {1} times in the array!", array[placeholder], maxSequence);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement