Advertisement
desislava_topuzakova

Problem 14. Мажорант на масив

Jun 6th, 2020
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace AlgorithmsOverStructureData
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. string input = Console.ReadLine(); //2 3 4 5 6 7 1
  12. List<int> numbers = input.Split(", ").Select(int.Parse).ToList();
  13. int n = numbers.Count;
  14. Dictionary<int, int> countNumbers = new Dictionary<int, int>();//число - брой пъти
  15.  
  16. foreach(int number in numbers)
  17. {
  18. if (countNumbers.ContainsKey(number))
  19. {
  20. countNumbers[number] += 1;
  21. }
  22. else
  23. {
  24. countNumbers.Add(number, 1);
  25. }
  26. }
  27.  
  28. foreach(var pair in countNumbers)
  29. {
  30. if (pair.Value >= n / 2 + 1)
  31. {
  32. Console.WriteLine(pair.Key);
  33. return;
  34. }
  35. }
  36.  
  37. Console.WriteLine("The majorant does not exists!");
  38. }
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement