K-th distinct element
Basic Accuracy: 33.37% Submissions: 69 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

Your Task:
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

 

to report an issue on this page.

Editorial

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

Yes

All Submissions

My Submissions:

Login to access your submissions.

Output Window