
- 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 check occurrences of every value is unique or not in Python
Suppose we have a list of numbers nums (positive or negative), we have to check whether the number of occurrences of every value in the array is unique or not.
So, if the input is like nums = [6, 4, 2, 9, 4, 2, 2, 9, 9, 9], then the output will be True, as there is 1 occurrence of 6, 2 occurrences of 4, 3 occurrences of 2, and 4 occurrences of 9. So all number of occurrences are unique.
To solve this, we will follow these steps −
num_counts := a new map where all values and number of occurrences of that value is stored
occurrences := list of all values of num_counts
return True when size of occurrences is same as number of unique elements in occurrences, otherwise false
Let us see the following implementation to get better understanding −
Example
from collections import Counter class Solution: def solve(self, nums): num_counts = dict(Counter(nums)) occurrences = num_counts.values() return len(occurrences) == len(set(occurrences)) ob = Solution() nums = [6, 4, 2, 9, 4, 2, 2, 9, 9, 9] print(ob.solve(nums))
Input
[6, 4, 2, 9, 4, 2, 2, 9, 9, 9]
Output
True
- Related Articles
- Program to check whether every rotation of a number is prime or not in Python
- How to check each value of a pandas series is unique or not?\n
- Program to check whether one value is present in BST or not in Python
- Program to check same value and frequency element is there or not in Python
- Program to check whether every one has at least a friend or not in Python
- Unique Number of Occurrences in Python
- Program to check whether each node value except leaves is sum of its children value or not in Python
- Program to check we can find three unique elements ose sum is same as k or not Python
- Program to check a string is palindrome or not in Python
- Program to check given string is pangram or not in Python
- Program to check given string is anagram of palindromic or not in Python
- Program to check a number is power of two or not in Python
- Program to check every sublist in a list containing at least one unique element in Python
- Program to check a number is ugly number or not in Python
- Program to check whether given graph is bipartite or not in Python

Advertisements