FastPrepMake All Elements Distinct (Amazon London)

Make All Elements Distinct (Amazon London)

Amazon logoAmazonEasyINTERNOA
Learn

Problem 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. 1. n: the number of products
  2. 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 = 3

Operations:

  1. Remove the first 2, resulting in [3, 2, 1, 5, 2]
  2. Remove the next 3, resulting in [2, 1, 5, 2]
  3. 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

See Amazon hiring insights
public int makeAllElementsDistinct(int n, int[] identifier) {
  // write your code here
}
n6
identifier[2, 3, 2, 1, 5, 2]
expected3
Checking account…