
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Check if the frequency of all the digits in a number is same in Python
Suppose we have a number num we have to check whether is balanced or not. A number is balanced when frequencies of all digits are same or not.
So, if the input is like num = 562256, then the output will be True as frequency of each digit is 2.
To solve this, we will follow these steps −
- number := convert num as string
- freq := a map containing frequencies of digits of number
- freq_values := make a new set by taking all digit frequency values from number
- if size of freq_values is same as 1, then
- return True
- return False
Let us see the following implementation to get better understanding −
Example Code
from collections import defaultdict def solve(num): number = str(num) freq = defaultdict(int) n = len(number) for i in range(n): freq[int(number[i])] += 1 freq_values = set(freq.values()) if len(freq_values) == 1: return True return False num = 562256 print(solve(num))
Input
562256
Output
True
- Related Articles
- Check if all digits of a number divide it in Python
- Check if frequency of all characters can become same by one removal in Python
- Python Program for Check if all digits of a number divide it
- Check if a string has all characters with same frequency with one variation allowed in Python
- Python - Check if all elements in a List are same
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string in Python
- C Program to Check if all digits of a number divide it
- PHP program to check if all digits of a number divide it
- Java Program to check if all digits of a number divide it
- Check if N is divisible by a number which is composed of the digits from the set {A, B} in Python
- Check if mirror image of a number is same if displayed in seven segment displays in Python
- Check if all bits of a number are set in Python
- C++ Program to check if a given number is Lucky (all digits are different)
- Check if product of digits of a number at even and odd places is equal in Python
- Check if frequency of each digit is less than the digit in Python

Advertisements