- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Write a program in Python to find the most repeated element in a series
Assume, you have the following series,
Series is: 0 1 1 22 2 3 3 4 4 22 5 5 6 22
And the result for the most repeated element is,
Repeated element is: 22
Solution
To solve this, we will follow the below approach,
Define a series
Set initial count is 0 and max_count value as series first element value data[0]
count = 0 max_count = data[0]
Create for loop to access series data and set frequency_count as l.count(i)
for i in data: frequency_count = l.count(i)
Set if condition to compare with max_count value, if the condition is true then assign count to frequency_count and change max_count to series present element. Finally, print the max_count. It is defined below,
if(frequency_count > max_count): count = frequency_count max_count = i print("Repeated element is:", max_count)
Example
Let’s see the below implementation to get a better understanding −
import pandas as pd l = [1,22,3,4,22,5,22] data = pd.Series(l) print("Series is:\n", data) count = 0 max_count = data[0] for i in data: frequency_count = l.count(i) if(frequency_count > max_count): count = frequency_count max_count = i print("Repeated element is:", max_count)
Output
Series is: 0 1 1 22 2 3 3 4 4 22 5 5 6 22 dtype: int64 Repeated element is: 22
Advertisements