Advertisement
CR7CR7

repeating-numbers

Jun 1st, 2023
821
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.96 KB | None | 0 0
  1. using System;
  2.  
  3. class Program
  4. {
  5.     static void Main()
  6.     {
  7.         int N = int.Parse(Console.ReadLine());
  8.  
  9.         int[] numbers = new int[N];
  10.         for (int i = 0; i < N; i++)
  11.         {
  12.             numbers[i] = int.Parse(Console.ReadLine());
  13.         }
  14.  
  15.         int[] frequency = new int[11]; // Array to store the frequency of each number (index 1 to 10)
  16.  
  17.         foreach (int num in numbers)
  18.         {
  19.             frequency[num]++;
  20.         }
  21.  
  22.         int maxFrequency = 0;
  23.         int mostFrequentNumber = int.MaxValue;
  24.  
  25.         for (int i = 1; i <= 10; i++)
  26.         {
  27.             if (frequency[i] > maxFrequency)
  28.             {
  29.                 maxFrequency = frequency[i];
  30.                 mostFrequentNumber = i;
  31.             }
  32.             else if (frequency[i] == maxFrequency && i < mostFrequentNumber)
  33.             {
  34.                 mostFrequentNumber = i;
  35.             }
  36.         }
  37.  
  38.         Console.WriteLine(mostFrequentNumber);
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement