k-th distinct (or non-repeating) element in an array.

Given an integer array, print k-th distinct element in an array. The given array may contain duplicates and the output should print k-th element among all unique elements. If k is more than number of distinct elements, print -1.

Examples:

Input : arr[] = {1, 2, 1, 3, 4, 2}, 
        k = 2
Output : 4
First non-repeating element is 3
Second non-repeating element is 4

Input : arr[] = {1, 2, 50, 10, 20, 2}, 
        k = 3
Output : 10

Input : {2, 2, 2, 2}, 
        k = 2
Output : -1

A simple solution is to use two nested loops where outer loop picks elements from left to right, and inner loop checks if the picked element is present somewhere else. If not present, then increment count of distinct elements. If count becomes k, return current element.

// C++ program to print k-th distinct
// element in a given array
#include <bits/stdc++.h>
using namespace std;

// Returns k-th distinct element in arr.
int printKDistinct(int arr[], int n, int k)
{
    int dist_count = 0;
    for (int i=0; i<n; i++)
    {
        // Check if current element is
        // present somewhere else.
        int j;
        for (j=0; j<n; j++)
            if (i != j && arr[j] == arr[i])
                break;

        // If element is unique
        if (j == n)
            dist_count++;

        if (dist_count == k)
            return arr[i];
    }

    return -1;
}

// Driver method to test above method
int main ()
{
    int ar[] = {1, 2, 1, 3, 4, 2};
    int n = sizeof(ar)/sizeof(ar[0]);
    int k = 2;
    cout << printKDistinct(ar, n, k);
    return 0;
}

Output:

4

 

An efficient solution is to use Hashing to solve this in O(n) time on average.

1) Create an empty hash table.
2) Traverse input array from left to right and store elements and their counts in the hash table.
3) Traverse input array again from left to right. Keep counting elements with count as 1.
4) If count becomes k, return current element.

// C++ program to print k-th distinct
// element in a given array
#include <bits/stdc++.h>
using namespace std;

// Returns k-th distinct element in arr.
int printKDistinct(int arr[],int n,int k)
{
    // Traverse input array and store
    // counts if individual elements.
    unordered_map<int, int> h;
    for (int i=0; i<n; i++)
        h[arr[i]]++;

    // If size of hash is less than k.
    if (h.size() < k)
        return -1;

    // Traverse array again and find
    // k-th element with count as 1.
    int dist_count = 0;
    for (int i=0; i<n; i++)
    {
        if (h[arr[i]] == 1)
            dist_count++;
        if (dist_count == k)
            return arr[i];
    }

    return -1;
}

// Driver method to test above method
int main ()
{
    int ar[] = {1, 2, 1, 3, 4, 2};
    int n = sizeof(ar)/sizeof(ar[0]);
    cout << printKDistinct(ar,n,2);
    return 0;
}

Output:

2

This article is contributed by Afzal Ansari. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice





Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.