k-th distinct (or non-repeating) element among unique elements in an array.
Given an integer array arr[], print kth distinct element in this array. The given array may contain duplicates and the output should print the k-th element among all unique elements. If k is more than the number of distinct elements, print -1.
Examples:
Input: arr[] = {1, 2, 1, 3, 4, 2}, k = 2
Output: 4
Explanation: The First non-repeating element is 3 and the Second non-repeating element is 4
Input: {2, 2, 2, 2}, k = 2
Output: -1
Explanation: There is no distinct (non-repeating) element in the array.Input: arr[] = {1, 2, 50, 10, 20, 2}, k = 3
Output: The First non-repeating element is 1 and the Second non-repeating element is 10.
Table of Content
[Naive Approach] Using Nested Loop – O(n^2) Time and O(1) Space
The idea is to use two nested loops where the outer loop picks elements from left to right, and the inner loop checks if the picked element is present somewhere else. If not present, then increment the count of distinct elements. If the count becomes k, return the current element.
#include <bits/stdc++.h>
using namespace std;
int printKDistinct(vector<int> arr,
int k)
{
int dist_count = 0, n = arr.size();
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;
}
int main ()
{
vector<int> arr = {1, 2, 1, 3, 4, 2};
int k = 2;
cout << printKDistinct(arr, k);
return 0;
}
class GfG
{
// Returns k-th distinct element in arr.
static int printKDistinct(int arr[],
int k)
{
int n = arr.length;
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;
}
public static void main (String[] args)
{
int arr[] = {1, 2, 1, 3, 4, 2};
int k = 2;
System.out.print(printKDistinct(arr, k));
}
}
def printKDistinct(arr, k):
dist_count = 0
n = len(arr)
for i in range(n):
# Check if current element is
# present somewhere else.
j = 0
while j < n:
if (i != j and arr[j] == arr[i]):
break
j += 1
# If element is unique
if (j == n):
dist_count += 1
if (dist_count == k):
return arr[i]
return -1
arr = [1, 2, 1, 3, 4, 2]
k = 2
print(printKDistinct(arr, k))
using System;
class GfG
{
static int printKDistinct(int []arr, int k)
{
int n = arr.Length;
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;
}
public static void Main ()
{
int []arr = {1, 2, 1, 3, 4, 2};
int k = 2;
Console.Write(printKDistinct(arr, k));
}
}
function printKDistinct(arr, k)
{
var dist_count = 0;
var n = arr.length;
for (var i = 0; i < n; i++)
{
// Check if current element is
// present somewhere else.
var 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;
}
var arr = [1, 2, 1, 3, 4, 2];
var k = 2;
console.log( printKDistinct(arr, k));
Output
4
[Expected Approach] Using Hashing – O(n) Time and O(n) Space
The idea is to traverse the array and use a hash table to store the the elements and their count of occurence. and then traverse the array again to find and count elements with occurence count equal to 1 and if count becomes k then return the current element.
#include <bits/stdc++.h>
using namespace std;
int printKDistinct(vector<int> arr, int k)
{
// Traverse input array and
// store counts if individual
// elements.
int n = arr.size();
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 Code
int main ()
{
vector<int>arr = {1, 2, 1, 3, 4, 2};
cout << printKDistinct(arr, 2);
return 0;
}
import java.util.*;
class GfG
{
static int printKDistinct(int arr[], int k)
{
//int dist_count = 0;
int n = arr.length;
Map <Integer, Integer> h =
new HashMap<Integer, Integer> ();
for (int i = 0; i < n; i++)
{
if(h.containsKey(arr[i]))
h.put(arr[i], h.get(arr[i]) + 1);
else
h.put(arr[i], 1);
}
// 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.get(arr[i]) == 1)
dist_count++;
if (dist_count == k)
return arr[i];
}
return -1;
}
public static void main (String[] args)
{
int arr[] = {1, 2, 1, 3, 4, 2};
System.out.println(printKDistinct(arr, 2));
}
}
def printKDistinct(arr, KthIndex):
size = len(arr)
dict = {}
vect = []
for i in range(size):
if(arr[i] in dict):
dict[arr[i]] = dict[arr[i]] + 1
else:
dict[arr[i]] = 1
for i in range(size):
if(dict[arr[i]] > 1):
continue
else:
KthIndex = KthIndex - 1
if(KthIndex == 0):
return arr[i]
return -1
arr = [1, 2, 1, 3, 4, 2]
print(printKDistinct(arr, 2))
using System;
using System.Collections.Generic;
class GfG
{
// Returns k-th distinct
// element in arr.
static int printKDistinct(int []arr, int k)
{
int n = arr.Length;
Dictionary<int, int> h = new Dictionary<int, int>();
for (int i = 0; i < n; i++)
{
if(h.ContainsKey(arr[i]))
{
var val = h[arr[i]];
h.Remove(arr[i]);
h.Add(arr[i], val + 1);
}
else
h.Add(arr[i], 1);
}
// If size of hash is
// less than k.
if (h.Count < 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 Code
public static void Main (String[] args)
{
int []arr = {1, 2, 1, 3, 4, 2};
Console.WriteLine(printKDistinct(arr, 2));
}
}
function printKDistinct(arr,k)
{
// int dist_count = 0;
let n = arr.length;
let h = new Map();
for (let i = 0; i < n; i++)
{
if(h.has(arr[i]))
h.set(arr[i], h.get(arr[i]) + 1);
else
h.set(arr[i], 1);
}
// If size of hash is
// less than k.
if (h.length < k)
return -1;
// Traverse array again and
// find k-th element with
// count as 1.
let dist_count = 0;
for (let i = 0; i < n; i++)
{
if (h.get(arr[i]) == 1)
dist_count++;
if (dist_count == k)
return arr[i];
}
return -1;
}
// Driver Code
let arr=[1, 2, 1, 3, 4, 2];
console.log(printKDistinct(arr, 2));
Output
4