Searching for the lowest repeated number

python

#1

Hi guys

I’ve been playing around with Python code trying to improve my skills lately. I’m trying to program my code to find the lowest commonly repeated number but I’ve been left scratching my head. So for example if i create a list with [3,5,5,5,6,7,8,6,8,8,9,9,7] as the integers, I’ll want 3 to be found as that is the least repeated number in this example list. I’m struggling to implement this from my head into a Python language.

Any pointers and tips will be greatly appreciated


#2

One thing that comes to mind is a frequency table. We can create a set of dictionary keys using the set() function, then iterate over the list and increment a count on each key.

>>> array = [3, 5, 5, 5, 6, 7, 8, 6, 8, 8, 9, 9, 7]
>>> keys = set(array)
>>> frequencies = { key: 0 for key in keys }
>>> frequencies
{3: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0}
>>> for key in array:
	frequencies[key] += 1

	
>>> frequencies
{3: 1, 5: 3, 6: 2, 7: 2, 8: 3, 9: 2}
>>> 

That’s the set up, now the table just needs to be analyzed. Let us know what you come up with for a solution.


#3

Thank-you, I will give this a go!