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
Selected Reading
Program to find an element in list whose value is same as its frequency in Python
Suppose we have a list of numbers called nums, we have to check whether there is an element whose frequency in the list is same as its value or not.
So, if the input is like [2, 4, 8, 10, 4, 4, 4], then the output will be True because the number 4 appears 4 times in the list.
Algorithm
To solve this, we will follow these steps ?
- Create a frequency map to store each element's count
- For each key-value pair (element, frequency) in the map, do
- If element equals its frequency, then return True
- If no such element found, return False
Using Dictionary with get() Method
Here's a clean implementation using the dictionary's get() method ?
def find_element_with_same_frequency(nums):
frequency = {}
# Count frequency of each element
for num in nums:
frequency[num] = frequency.get(num, 0) + 1
# Check if any element equals its frequency
for element, count in frequency.items():
if element == count:
return True
return False
# Test the function
numbers = [2, 4, 8, 10, 4, 4, 4]
result = find_element_with_same_frequency(numbers)
print(f"Input: {numbers}")
print(f"Output: {result}")
Input: [2, 4, 8, 10, 4, 4, 4] Output: True
Using Counter from Collections
We can also use Python's Counter class for a more concise solution ?
from collections import Counter
def find_element_using_counter(nums):
frequency = Counter(nums)
for element, count in frequency.items():
if element == count:
return True
return False
# Test with different examples
test_cases = [
[2, 4, 8, 10, 4, 4, 4], # 4 appears 4 times - True
[1, 2, 3, 3, 3], # 3 appears 3 times - True
[1, 2, 5, 6], # No match - False
[1, 1, 2, 2, 2, 3, 3, 3, 3] # Multiple matches - True
]
for nums in test_cases:
result = find_element_using_counter(nums)
print(f"Input: {nums}")
print(f"Output: {result}")
print()
Input: [2, 4, 8, 10, 4, 4, 4] Output: True Input: [1, 2, 3, 3, 3] Output: True Input: [1, 2, 5, 6] Output: False Input: [1, 1, 2, 2, 2, 3, 3, 3, 3] Output: True
One-liner Solution
For a more compact approach using any() function ?
from collections import Counter
def find_element_oneliner(nums):
freq = Counter(nums)
return any(element == count for element, count in freq.items())
# Test the one-liner
numbers = [2, 4, 8, 10, 4, 4, 4]
result = find_element_oneliner(numbers)
print(f"Result: {result}")
Result: True
Comparison
| Method | Readability | Performance | Best For |
|---|---|---|---|
| Dictionary with get() | High | O(n) | Learning purposes |
| Counter | High | O(n) | Production code |
| One-liner with any() | Medium | O(n) | Competitive programming |
Conclusion
All three methods have O(n) time complexity and solve the problem efficiently. Use Counter for cleaner code or the dictionary approach for better understanding of the frequency counting process.
Advertisements
