Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Напишете програма която отпечатва най-често срещаното число в масив. Числата в масива са в границите от 0 до 1023.
- Пример: { 4, 1, 1, 4, 2, 3, 4, 4, 1, 2, 4, 9, 3 } -> 4 (5 times) */
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace _07_Repeated
- {
- class Repeated
- {
- static void Main()
- {
- //Console.Write("Numbers of array = ");
- //int n = Int32.Parse(Console.ReadLine());
- //int[] array = new int[n];
- //for (int i = 0; i < n; i++)
- //{
- // Console.Write("Element {0} = ", i + 1);
- // array[i] = Int32.Parse(Console.ReadLine());
- //}
- int[] array = { 4, 1, 1, 4, 2, 3, 4, 4, 1, 2, 4, 9, 3 };
- int[] occurrencses = new int[1024];
- for (int i = 0; i < array.Length; i++)
- {
- int n = array[i];
- occurrencses[n]++;
- }
- int mostCommonNumber = 0;
- for (int i = 1; i < occurrencses.Length; i++)
- {
- if (occurrencses[i] > occurrencses[mostCommonNumber])
- {
- mostCommonNumber = i;
- }
- }
- Console.WriteLine("The most common number is {0} and it occurs {1} times",
- mostCommonNumber,occurrencses[mostCommonNumber]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment