FastPrepKeep the First Key for Each Distinct Value

Keep the First Key for Each Distinct Value

OneTrust logoOneTrust● EasyFULLTIMEONSITE INTERVIEW
Learn

Problem statement

Two equal-length arrays describe an insertion-ordered object: keys[i] is paired with values[i]. Remove every later entry whose value has already appeared, and return the retained keys in their original order.

Function

keepFirstKeysByValue(keys: String[], values: int[]) → String[]

Examples

Example 1

keys = ["a","b","c","d"]values = [1,2,3,1]return = ["a","b","c"]

The later key d repeats value one, so only the first key for that value remains.

Example 2

keys = ["a","b","c"]values = [1,1,1]return = ["a"]

All entries share one value, so the first key is the only retained key.

Constraints

  • 1 <= keys.length == values.length <= 200000
  • keys are distinct nonempty strings
  • values contain signed 32-bit integers

More OneTrust problems

See OneTrust hiring insights
public String[] keepFirstKeysByValue(String[] keys, int[] values) {
    // Write your code here.
}
keys["a","b","c","d"]
values[1,2,3,1]
expected["a", "b", "c"]
Checking account…