X DAYS

:

HOUR

:

MINS

:

SEC

Copied to Clipboard
K-th distinct element
Basic Accuracy: 50.05% Submissions: 1147 Points: 1

Given an array of n integers. Find the kth distinct element.

Example 1:

Input:
n = 6, k = 2
arr = {1, 2, 1, 3, 4, 2}
Output:
4
Explanation: We initially have 1, the
first distinct element is 2, and the
second distinct element is 4.

Example 2:

Input:
n = 6, k = 3
arr = {1, 2, 50, 10, 20, 2}
Output:
10

You don't need to read or print anything. Your task is to complete the function KthDistinct() which takes the array of elements as input parameter and returns the kth distinct element. If not possible return -1.

Expected Time Complexity: O(n)
Expected Space Complexity: O(n)

Constranits:
1 <= length of array <= 105
1 <= elements of array <= 106

We are replacing the old Disqus forum with the new Discussions section given below.

Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

My Submissions:  