FastPrepFilter Duplicates While Preserving Order

Filter Duplicates While Preserving Order

Upstart logoUpstart● EasyFULLTIMEOA
Learn

Problem statement

Given an integer array data, return a new array containing each distinct value exactly once.

Preserve the order of first appearance: when a value occurs multiple times, keep only its earliest occurrence.

Function

filterDuplicates(data: int[]) → int[]

Examples

Example 1

data = [7,6,4,3,3,4,9]return = [7,6,4,3,9]

The second 3 and second 4 are removed. Every retained value stays in its first-appearance position.

Example 2

data = [5,5,5]return = [5]

Only the first occurrence of 5 is retained.

Example 3

data = [-2,0,-2,4,0]return = [-2,0,4]

Negative values and zero follow the same first-occurrence rule.

Constraints

  • data is not null.
  • Every element of data is an integer.

More Upstart problems

See Upstart hiring insights
public int[] filterDuplicates(int[] data) {
    // write your code here
}
data[7,6,4,3,3,4,9]
expected[7,6,4,3,9]
Checking account…