Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Find Kth Duplicate in Array

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Find Kth Duplicate in Array. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Hp IQ telemetry processors may need to identify repeated event values in the order they first become known to be duplicates. Given an integer array and an integer k, return the kth distinct value that appears at least twice, ordered by its first appearance in the array. Count each duplicated value once, even if it appears more than twice. Return -1 if fewer than k distinct values are duplicated.

Formal Specification

Implement a function that accepts nums, a non-empty list of integers, and k, a positive integer. Return an integer representing the kth distinct duplicated value, or -1 when no such value exists. The output order is based on each value's first index in nums, not on frequency or the index of its second occurrence.

Constraints

  • 1 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • 1 <= k <= nums.length
  • A duplicated value is counted once, regardless of its frequency

Function Signature

def kth_duplicate(nums, k):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output