'Code in Python that count repeated elements and return the item repeated most times and how many times
I have a list with integers, I need to find which item repeat the most, then the function should return two variables (value, number) value is the repeated element and number is the times that repeated
input
B = [1, 5, 1, 3, 5, 1]
output
1 3
I got a code that works with the first item in the list, but when it goes to the next loop it start everything in zero, so I was struggling to store what I got in the first loop and compare with the second loop to see which one I need to keep.
For this function I am not allowed to use any module or library like "count" or any other advanced code, just as simple as it can be for a rookie level
def counting(B):
n = len(B)
tempcount = []
tempcval = []
for i in range(0,n-1):
cval=B[i]
count = 0
for next in B:
if next == cval:
count += 1
tempcount.append(count)
tempcval.append(cval)
return count,cval
def main():
list = [1, 5, 1, 3, 5, 1]
a,b = counting(list)
print(a,b)
main()
expected output
3 1
actual output
2 5
Solution 1:[1]
A good solution could be to use collections.Counter
:
>>> import collections
>>> c = collections.Counter([1, 5, 1, 3, 5, 1])
>>> c
Counter({1: 3, 5: 2, 3: 1})
>>> c.most_common(1)
[(1, 3)]
In a function this could look like this:
import collections
def counting(the_list):
c = collections.Counter(the_list)
data = c.most_common(1)[0]
print('value', data[0])
print('count', data[1])
return data[1], data[0]
Solution 2:[2]
Since I'm familiar with pandas, dataframe manipulating library
If I were you, I will use
import pandas as pd
temp = pd.Series([1, 5, 1, 3, 5, 1])
temp2 = temp.value_counts()
temp2.index[0], temp2.iloc[0]
But the first answer seems more simple hahaha
Solution 3:[3]
A solution using nothing requiring any imports. Your code had some issues in that you never actually used those tempcount and tempcvals. You'd append to them, but nothing else. I took them along for the ride this time and am using them to store the counts of each value, basically turning tempcval into a non duplicate list of the inputted list.
def counting(B):
n = len(B)
tempcount = []
tempcval = []
for next in B:
if next in tempcval:
point = tempcval.index(next)
tempcount[point] = tempcount[point] + 1
else:
tempcval.append(next)
tempcount.append(1)
maxCount = max(tempcount)
point = tempcount.index(maxCount)
return tempcount[point],tempcval[point]
def main():
list = [1, 5, 1, 3, 5, 1]
a,b = counting(list)
print(a,b)
main()
So there is one issue with this code. It works in the example given, but say you gave
list = [1, 5, 1, 3, 5, 1, 5]
the code would still return 3 1 since the 1 appeared before the 5.
Solution 4:[4]
This question looks to be solved, but here's a spin using the collections.Counter
method:
from collections import Counter
my_list = [1, 5, 1, 3, 5, 1, 5]
my_list_counted = Counter(my_list)
most_common_items = my_list_counted.most_common(len(my_list_counted))
print(most_common_items[0][0], most_common_items[-1][0])
>>> 1 3
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Ralf |
Solution 2 | Noppu |
Solution 3 | |
Solution 4 | irahorecka |