Created
May 25, 2020 18:07
-
-
Save jigi-33/24f42356ceffa596cb81a2ee3b24d7f5 to your computer and use it in GitHub Desktop.
Самые часто встречаемые элементы заданного массива с помощью Counter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
ЧАСТЫЙ (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