Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Check if the frequency of all the digits in a number is same in Python
A number is considered balanced when the frequency of all its digits is the same. For example, in the number 562256, each digit (5, 6, and 2) appears exactly 2 times, making it a balanced number.
In this article, we'll explore different methods to check if a number is balanced in Python.
Using Dictionary to Count Frequencies
We can convert the number to a string and use a dictionary to count the frequency of each digit ?
from collections import defaultdict
def is_balanced(num):
number = str(num)
# Count frequency of each digit
freq = defaultdict(int)
for digit in number:
freq[digit] += 1
# Get all frequency values
freq_values = set(freq.values())
# If all digits have same frequency, set will have only one element
return len(freq_values) == 1
# Test the function
num = 562256
print(f"Number: {num}")
print(f"Is balanced: {is_balanced(num)}")
Number: 562256 Is balanced: True
Using Counter from Collections
The Counter class provides a more concise way to count digit frequencies ?
from collections import Counter
def is_balanced_counter(num):
# Count frequencies of all digits
digit_counts = Counter(str(num))
# Get unique frequency values
frequencies = set(digit_counts.values())
# Return True if all digits have same frequency
return len(frequencies) == 1
# Test with multiple examples
test_numbers = [562256, 112233, 123456, 1122]
for num in test_numbers:
result = is_balanced_counter(num)
print(f"{num}: {'Balanced' if result else 'Not Balanced'}")
562256: Balanced 112233: Balanced 123456: Balanced 1122: Balanced
Using List Comprehension
A more compact approach using list comprehension and set operations ?
def is_balanced_compact(num):
num_str = str(num)
# Count frequency of each unique digit
frequencies = [num_str.count(digit) for digit in set(num_str)]
# Check if all frequencies are the same
return len(set(frequencies)) == 1
# Test with unbalanced number
test_cases = [562256, 12233, 111222, 12345]
for num in test_cases:
result = is_balanced_compact(num)
digit_counts = {digit: str(num).count(digit) for digit in set(str(num))}
print(f"Number: {num}")
print(f"Digit frequencies: {digit_counts}")
print(f"Result: {'Balanced' if result else 'Not Balanced'}")
print("-" * 30)
Number: 562256
Digit frequencies: {'5': 2, '2': 2, '6': 2}
Result: Balanced
------------------------------
Number: 12233
Digit frequencies: {'1': 1, '2': 2, '3': 2}
Result: Not Balanced
------------------------------
Number: 111222
Digit frequencies: {'1': 3, '2': 3}
Result: Balanced
------------------------------
Number: 12345
Digit frequencies: {'1': 1, '2': 1, '4': 1, '5': 1, '3': 1}
Result: Balanced
------------------------------
Comparison of Methods
| Method | Time Complexity | Readability | Best For |
|---|---|---|---|
| Dictionary | O(n) | High | Educational purposes |
| Counter | O(n) | Very High | Production code |
| List Comprehension | O(n²) | Medium | Compact solutions |
Conclusion
To check if a number is balanced, count the frequency of each digit and verify all frequencies are equal. The Counter method offers the cleanest solution, while the dictionary approach provides better understanding of the algorithm.
