- Related Questions & Answers
- Unique number of occurrences of elements in an array in JavaScript
- Program to check occurrences of every value is unique or not in Python
- Largest Unique Number in Python
- How to count the number of occurrences of all unique values in an R data frame?
- How to find the number of occurrences of unique and repeated characters in a string vector in R?
- Product of unique prime factors of a number in Python Program
- Maximum Number of Occurrences of a Substring in C++
- Python Program for Product of unique prime factors of a number
- How to count total number of occurrences of an object in a Python list?
- Python Program to Find Number of Occurrences of All Elements in a Linked List
- Finding number of occurrences of a specific string in MySQL?
- Finding number of occurrences of the element appearing most number of times in JavaScript
- Assign value to unique number in list in Python
- Occurrences After Bigram in Python
- MySQL Aggregate Function to find the number of occurrences?

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have an array. We have to check whether each element has unique number of occurrences. If no such element is present then return false, otherwise true. So if the array is like [1, 1, 2, 2, 2, 3, 4, 4, 4, 4], then it will return true as the element 1 is present two times, 2 is present three times, 3 is present once and 4 is present four times.

To solve this, we will follow these steps −

- We will find the frequency of elements of the array
- for each key-value pair in the frequency map
- if value is present in another map mp, then return false
- put mp[value] := 1

- return true

Let us see the following implementation to get better understanding −

class Solution(object): def uniqueOccurrences(self, arr): d = {} for i in arr: if i not in d: d[i] =1 else: d[i]+=1 l = {} for x, y in d.items(): if y in l: return False l[y] = 1 return True ob1 = Solution() print(ob1.uniqueOccurrences([1,1,2,2,2,3,4,4,4,4]))

[1,1,2,2,2,3,4,4,4,4]

true

Advertisements