Most Frequent Element | Challenge

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this challenge, you are required to write a Python function that takes a list of integers as input and returns the most frequent element in the list.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ErrorandExceptionHandlingGroup(["`Error and Exception Handling`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/numeric_types("`Numeric Types`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ErrorandExceptionHandlingGroup -.-> python/custom_exceptions("`Custom Exceptions`") python/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/variables_data_types -.-> lab-13165{{"`Most Frequent Element | Challenge`"}} python/numeric_types -.-> lab-13165{{"`Most Frequent Element | Challenge`"}} python/lists -.-> lab-13165{{"`Most Frequent Element | Challenge`"}} python/tuples -.-> lab-13165{{"`Most Frequent Element | Challenge`"}} python/function_definition -.-> lab-13165{{"`Most Frequent Element | Challenge`"}} python/custom_exceptions -.-> lab-13165{{"`Most Frequent Element | Challenge`"}} python/data_collections -.-> lab-13165{{"`Most Frequent Element | Challenge`"}} python/build_in_functions -.-> lab-13165{{"`Most Frequent Element | Challenge`"}} end

Most Frequent Element

Problem

Write a Python function called most_frequent(lst) that takes a list of integers as input and returns the most frequent element in the list. If there are multiple elements that appear the same number of times and have the highest frequency, return the one that appears first in the list.

To solve this problem, you can follow these steps:

  1. Use set() to get the unique values in lst.
  2. Use max() to find the element that has the most appearances.

Your function should have the following signature:

def most_frequent(lst: List[int]) -> int:

Example

assert most_frequent([1, 2, 1, 2, 3, 2, 1, 4, 2]) == 2
assert most_frequent([1, 2, 3, 4, 5]) == 1
assert most_frequent([1, 1, 1, 1, 1]) == 1

Summary

In this challenge, you have learned how to find the most frequent element in a list of integers using Python. You have also learned how to use the set() and max() functions to solve this problem.

Other Python Tutorials you may like