Dsa
Two Pointers
Medium
Kth Distinct Element
Given an array of integers, find the k-th distinct element in the array. If k is greater than the number of distinct elements, return -1.
Input:
The first line contains two integers n (1 <= n <= 1000) and k (1 <= k <= n).
The second line contains n integers, the elements of the array.
Output:
An integer representing the k-th distinct element, or -1 if it does not exist.
Example:
Input:
5 2
1 2 1 3 4
Output:
3
Explanation: The distinct elements are [1, 2, 3, 4], and the second distinct element is 3.
Key concepts
two_pointersarraydistinct
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now