
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Element in List with Same Value as 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
Advertisements