Skip to content

Instantly share code, notes, and snippets.

@jigi-33
Created May 25, 2020 18:07
Show Gist options
  • Save jigi-33/24f42356ceffa596cb81a2ee3b24d7f5 to your computer and use it in GitHub Desktop.
Save jigi-33/24f42356ceffa596cb81a2ee3b24d7f5 to your computer and use it in GitHub Desktop.
Самые часто встречаемые элементы заданного массива с помощью Counter
"""
ЧАСТЫЙ (MOST COMMON) ЭЛЕМЕНТ ИЗ МАССИВА
"""
# Задание: Дан массив a из n целых чисел. Напишите код, который найдет число, что чаще других встречается в массиве.
# Формат вывода: единственное число x, наибольшее из чисел, которое чаще других встречается в массиве a.
# Решение: задачу можно решить как с использованием обычного словаря, так и с использованием структуры Counter из модуля collections:
from collections import Counter
from operator import itemgetter
a = [0, 0, 10, 10]
counter = Counter(a)
result = max(counter.items(), key=itemgetter(1, 0))[0]
# это наиболее питоническое решение :)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment