
- 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
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
To solve this, we will follow these steps −
- res := a new map to store value wise frequency
- for each key value pair (k,v) in res, do
- if k is same as v, then
- return True
- if k is same as v, then
- return False
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, nums): res = {} for i in nums: try: res[i] += 1 except: res[i] = 1 for k,v in res.items(): if k == v: return True return False ob = Solution() print(ob.solve([2, 4, 8, 10, 4, 4, 4]))
Input
[2, 4, 8, 10, 4, 4, 4]
Output
True
- Related Articles
- Program to remove all nodes of a linked list whose value is same as in Python
- Program to check same value and frequency element is there or not in Python
- Program to find minimum number of subsequence whose concatenation is same as target in python
- C++ program to find range whose sum is same as n
- Program to find length of shortest sublist with maximum frequent element with same frequency in Python
- Python Program to assign each list element value equal to its magnitude order
- Program to find smallest index for which array element is also same as index in Python
- Program to check we can find four elements whose sum is same as k or not in Python
- Program to find number of K-Length sublists whose average is greater or same as target in python
- Array range queries for elements with frequency same as value in C Program?
- Element with largest frequency in list in Python
- Program to find frequency of the most frequent element in Python
- Program to create data structure to check pair sum is same as value in Python
- Write a Java program to find the first array element whose value is repeated an integer array?
- Write a Golang program to find the frequency of an element in an array

Advertisements