Geeksforgeeks

X

DAYS

:

HOUR

:

MINS

:

SEC

Error

Copied to Clipboard

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:4Explanation: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 <= 10^{5}

1 <= elements of array <= 10^{6}

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

Click here to view old Disqus comments.

Click here to view old Disqus comments.

Login to report an issue on this page.

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

YesLoading...

K-th distinct element

...