给定一个整数数组,打印数组中的第 k 个不同元素。给定的数组可能包含重复项,输出应打印所有唯一元素中的第 k 个元素。如果 k 大于不同元素的数量,则打印 -1。
例子 :
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
一个简单的解决方案是使用两个嵌套循环,其中外循环从左到右选取元素,内循环检查选取的元素是否存在于其他地方。如果不存在,则增加不同元素的计数。如果 count 变为 k,则返回当前元素。
C++
// C++ program to print k-th distinct
// element in a given array
#include
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 Code
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;
}
Java
// Java program to print k-th distinct
// element in a given array
class GFG
{
// Returns k-th distinct element in arr.
static 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 code
public static void main (String[] args)
{
int ar[] = {1, 2, 1, 3, 4, 2};
int n = ar.length;
int k = 2;
System.out.print(printKDistinct(ar, n, k));
}
}
// This code is contributed by Anant Agarwal.
Python3
# Python3 program to prk-th distinct
# element in a given array
# Returns k-th distinct
# element in arr.
def printKDistinct(arr, n, k):
dist_count = 0
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
# Driver Code
ar = [1, 2, 1, 3, 4, 2]
n = len(ar)
k = 2
print(printKDistinct(ar, n, k))
# This code is contributed by Mohit Kumar
C#
// C# program to print k-th distinct
// element in a given array
using System;
class GFG
{
// Returns k-th distinct element in arr
static 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 code
public static void Main ()
{
int []ar = {1, 2, 1, 3, 4, 2};
int n = ar.Length;
int k = 2;
Console.Write(printKDistinct(ar, n, k));
}
}
// This code is contributed by nitn mittal
PHP
Javascript
C++
// C++ program to print k-th
// distinct element in a
// given array
#include
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 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 ()
{
int ar[] = {1, 2, 1, 3, 4, 2};
int n = sizeof(ar) / sizeof(ar[0]);
cout << printKDistinct(ar, n, 2);
return 0;
}
Java
// Java program to print k-th distinct
// element in a given array
import java.util.*;
class GfG
{
// Returns k-th distinct
// element in arr.
static int printKDistinct(int arr[],
int n, int k)
{
//int dist_count = 0;
Map h =
new HashMap ();
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;
}
// Driver Code
public static void main (String[] args)
{
int ar[] = {1, 2, 1, 3, 4, 2};
int n = ar.length;
System.out.println(printKDistinct(ar, n, 2));
}
}
// This code is contributed by
// Prerna Saini
Python3
# Python3 program to print k-th
# distinct element in a given array
def printKDistinct(arr, size, KthIndex):
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
# Driver Code
arr = [1, 2, 1, 3, 4, 2]
size = len(arr)
print(printKDistinct(arr, size, 2))
# This code is contributed
# by Akhand Pratap Singh
C#
// C# program to print k-th distinct
// element in a given array
using System;
using System.Collections.Generic;
class GfG
{
// Returns k-th distinct
// element in arr.
static int printKDistinct(int []arr,
int n, int k)
{
Dictionary h = new Dictionary();
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 []ar = {1, 2, 1, 3, 4, 2};
int n = ar.Length;
Console.WriteLine(printKDistinct(ar, n, 2));
}
}
// This code contributed by Rajput-Ji
Javascript
输出 :
4
一个有效的解决方案是使用哈希在平均 O(n) 时间内解决这个问题。
1)创建一个空的哈希表。
2)从左到右遍历输入数组并将元素及其计数存储在哈希表中。
3)再次从左到右遍历输入数组。继续计数计数为 1 的元素。
4)如果count 变为k,则返回当前元素。
C++
// C++ program to print k-th
// distinct element in a
// given array
#include
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 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 ()
{
int ar[] = {1, 2, 1, 3, 4, 2};
int n = sizeof(ar) / sizeof(ar[0]);
cout << printKDistinct(ar, n, 2);
return 0;
}
Java
// Java program to print k-th distinct
// element in a given array
import java.util.*;
class GfG
{
// Returns k-th distinct
// element in arr.
static int printKDistinct(int arr[],
int n, int k)
{
//int dist_count = 0;
Map h =
new HashMap ();
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;
}
// Driver Code
public static void main (String[] args)
{
int ar[] = {1, 2, 1, 3, 4, 2};
int n = ar.length;
System.out.println(printKDistinct(ar, n, 2));
}
}
// This code is contributed by
// Prerna Saini
蟒蛇3
# Python3 program to print k-th
# distinct element in a given array
def printKDistinct(arr, size, KthIndex):
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
# Driver Code
arr = [1, 2, 1, 3, 4, 2]
size = len(arr)
print(printKDistinct(arr, size, 2))
# This code is contributed
# by Akhand Pratap Singh
C#
// C# program to print k-th distinct
// element in a given array
using System;
using System.Collections.Generic;
class GfG
{
// Returns k-th distinct
// element in arr.
static int printKDistinct(int []arr,
int n, int k)
{
Dictionary h = new Dictionary();
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 []ar = {1, 2, 1, 3, 4, 2};
int n = ar.Length;
Console.WriteLine(printKDistinct(ar, n, 2));
}
}
// This code contributed by Rajput-Ji
Javascript
输出 :
4
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。