Make All Elements Distinct (Amazon London)
Learn this problemProblem statement
An Amazon warehouse manager is responsible for managing inventory and ensuring that each product has a unique identifier. There are n products in the warehouse, where the identifier of the i-th item is represented by the array identifier[i]. However, in the inventory management system, some items have the same identifier.
To make all items in the inventory distinct, the following operation can be used:
- Remove the first (leftmost) item from the inventory sequence.
Given n products and the array identifier, find the minimum number of operations required to make all items in the inventory distinct.
Function
makeAllElementsDistinct(n: int, identifier: int[]) → int
Complete the function makeAllElementsDistinct in the editor.
makeAllElementsDistinct has the following parameters:
- 1.
n: the number of products - 2.
int identifier[n]: an array of integers representing the identifiers
Returns
int: the minimum number of operations required to make all the elements of the array distinct
Examples
Example 1
n = 6identifier = [2, 3, 2, 1, 5, 2]return = 3Operations:
- Remove the first 2, resulting in [3, 2, 1, 5, 2]
- Remove the next 3, resulting in [2, 1, 5, 2]
- Remove the next 2, resulting in [1, 5, 2]
After 3 operations, identifier = [1, 5, 2], and all the elements of identifier are distinct.
Hence, the minimum number of operations required to make all the elements of the array distinct is 3.
Constraints
🍑🍑More Amazon problems
- Secure Maximum DeliveriesOA · Seen Jul 2026
- Find Median from Data StreamONSITE INTERVIEW · Seen Jul 2026
- Handwritten SigmoidPHONE SCREEN · Seen Jul 2026
- Handwritten SoftmaxPHONE SCREEN · Seen Jul 2026
- Koko Eating BananasONSITE INTERVIEW · Seen Jul 2026
- Loyal Customers Across Two DaysONSITE INTERVIEW · Seen Jul 2026
- Maximum System Memory CapacityOA · Seen Jul 2026
- Package Delivery SystemOA · Seen Jul 2026