Freq Digits
Given an integer number, find the most frequent digit in it.
-
how to count things efficiently?
-
keep calm and code in Python!
Let use the statistics that comes with python stand library
Statistic Module
This module provides functions for calculating mathematical statistics of numeric (Real-valued) data.
import statistics
from statistics import mode
This module statistics offers a Single mode (most common value) of discrete or nominal data. The values or method to find this is to convert the vaules into a string and then back again once the frequent digit is found
def freq_digit(num: int) -> int:
numbers = list(str(num))
return int(mode(numbers))
Trying out a value
Next we want to test a value
num_list = 12345
Finally
we can run the function with some additional values and input
freq_digit(num_list)
1
freq_digit(12334433)
3
freq_digit(773283277)
7
freq_digit(1121)
1
Read other posts