BorislavBorisov

Редици.04.01.Most frequent number in int array

Oct 11th, 2015
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.32 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. class MostFrequentNumber
  4. {
  5.     static void Main()
  6.     {
  7.         int[] arr = { 1, 1, 55, 55, 1,55,55 };
  8.         List<int> list = new List<int>();
  9.  
  10.         int appearance = 1;
  11.         int bestAppearance = 0;
  12.         int value = 0;
  13.         for (int i = 0; i < arr.Length; i++)
  14.         {
  15.             if (list.Contains(arr[i]))
  16.             {
  17.                 continue;
  18.             }
  19.             for (int j = i + 1; j < arr.Length; j++)
  20.             {
  21.                 if (arr[i] == arr[j])
  22.                 {
  23.                     appearance++;
  24.                     if (appearance > bestAppearance)
  25.                     {
  26.                         bestAppearance = appearance;
  27.                         value = arr[i];
  28.                     }
  29.                 }
  30.             }
  31.             //if (appearance > bestAppearance)
  32.             //{
  33.             //    bestAppearance = appearance;
  34.             //    value = arr[i];
  35.             //}
  36.             appearance = 1;
  37.             list.Add(arr[i]);  //добавям ги в листа и проверявам за да не се повтаря търсенето          
  38.         }
  39.         Console.Write("Best times -> " + bestAppearance);//4
  40.         Console.WriteLine(" Value -> " + value);//5
  41.         Console.WriteLine();
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment