Check whether every value in an array has a different number of occurrences

Quick Overview

Given an array of integers, decide whether each distinct value appears a different number of times from every other value. The problem tests frequency counting with a hash map and comparing a collection of counts for repeats.

Check whether every value in an array has a different number of occurrences

Company: Unity

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given an array of integers `arr`, return `True` if the number of occurrences of each value in the array is unique, or `False` otherwise. ### Function Signature ```python def unique_occurrences(arr: list[int]) -> bool: ``` ### Rules - Count how many times each distinct value appears in `arr`. Return `True` exactly when no two different values have the same count. - An array with a single distinct value always returns `True`. ### Constraints - `1 <= len(arr) <= 1000` - `-1000 <= arr[i] <= 1000` ### Examples ```text Input: arr = [1,2,2,1,1,3] Output: True ``` The value 1 has 3 occurrences, 2 has 2 occurrences, and 3 has 1 occurrence. The counts 3, 2 and 1 are all different. ```text Input: arr = [1,2] Output: False ``` The values 1 and 2 each have 1 occurrence, so two values share the count 1. ```text Input: arr = [-5,0,-5,0,-5,8] Output: True ``` The value -5 appears 3 times, 0 appears 2 times, and 8 appears once. The counts 3, 2 and 1 are all different.

Overview: Given an array of integers, decide whether each distinct value appears a different number of times from every other value. The problem tests frequency counting with a hash map and comparing a collection of counts for repeats.

Unity logo
Unity
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given an array of integers arr, return True if the number of occurrences of each value in the array is unique, or False otherwise.

Function Signature

def unique_occurrences(arr: list[int]) -> bool:

Rules

  • Count how many times each distinct value appears in arr . Return True exactly when no two different values have the same count.
  • An array with a single distinct value always returns True .

Constraints

  • 1 <= len(arr) <= 1000
  • -1000 <= arr[i] <= 1000

Examples

Input: arr = [1,2,2,1,1,3]
Output: True

The value 1 has 3 occurrences, 2 has 2 occurrences, and 3 has 1 occurrence. The counts 3, 2 and 1 are all different.

Input: arr = [1,2]
Output: False

The values 1 and 2 each have 1 occurrence, so two values share the count 1.

Input: arr = [-5,0,-5,0,-5,8]
Output: True

The value -5 appears 3 times, 0 appears 2 times, and 8 appears once. The counts 3, 2 and 1 are all different.