给定一个由 n 个整数组成的数组。任务是找到出现 k 次的第一个元素。如果没有元素出现k次打印-1。整数元素的分布可以在任何范围内。
例子:
Input: {1, 7, 4, 3, 4, 8, 7},
k = 2
Output: 7
Both 7 and 4 occur 2 times.
But 7 is the first that occurs 2 times.
Input: {4, 1, 6, 1, 6, 4},
k = 1
Output: -1
简单的方法:通过使用两个循环,计算一个数字在数组中出现的次数。
时间复杂度:O(n 2 )。
有效方法:使用 unordered_map 进行散列,因为范围未知。脚步:
- 从左到右遍历数组元素。
- 在遍历时增加它们在哈希表中的计数。
- 再次从左到右遍历数组并检查哪个元素的计数等于 k。打印该元素并停止。
- 如果没有元素的计数等于 k,则打印 -1。
以下是上述方法的试运行:
下面是上述方法的实现:
C++
// C++ implementation to find first
// element occurring k times
#include
using namespace std;
// function to find the first element
// occurring k number of times
int firstElement(int arr[], int n, int k)
{
// unordered_map to count
// occurrences of each element
unordered_map count_map;
for (int i=0; i
Java
import java.util.HashMap;
// Java implementation to find first
// element occurring k times
class GFG {
// function to find the first element
// occurring k number of times
static int firstElement(int arr[], int n, int k) {
// unordered_map to count
// occurrences of each element
HashMap count_map = new HashMap<>();
for (int i = 0; i < n; i++) {
int a = 0;
if(count_map.get(arr[i])!=null){
a = count_map.get(arr[i]);
}
count_map.put(arr[i], a+1);
}
//count_map[arr[i]]++;
for (int i = 0; i < n; i++) // if count of element == k ,then
// it is the required first element
{
if (count_map.get(arr[i]) == k) {
return arr[i];
}
}
// no element occurs k times
return -1;
}
// Driver program to test above
public static void main(String[] args) {
int arr[] = {1, 7, 4, 3, 4, 8, 7};
int n = arr.length;
int k = 2;
System.out.println(firstElement(arr, n, k));
}
}
//this code contributed by Rajput-Ji
Python3
# Python3 implementation to
# find first element
# occurring k times
# function to find the
# first element occurring
# k number of times
def firstElement(arr, n, k):
# dictionary to count
# occurrences of
# each element
count_map = {};
for i in range(0, n):
if(arr[i] in count_map.keys()):
count_map[arr[i]] += 1
else:
count_map[arr[i]] = 1
i += 1
for i in range(0, n):
# if count of element == k ,
# then it is the required
# first element
if (count_map[arr[i]] == k):
return arr[i]
i += 1
# no element occurs k times
return -1
# Driver Code
if __name__=="__main__":
arr = [1, 7, 4, 3, 4, 8, 7];
n = len(arr)
k = 2
print(firstElement(arr, n, k))
# This code is contributed
# by Abhishek Sharma
C#
// C# implementation to find first
// element occurring k times
using System;
using System.Collections.Generic;
class GFG
{
// function to find the first element
// occurring k number of times
static int firstElement(int []arr, int n, int k)
{
// unordered_map to count
// occurrences of each element
Dictionary count_map = new Dictionary();
for (int i = 0; i < n; i++)
{
int a = 0;
if(count_map.ContainsKey(arr[i]))
{
a = count_map[arr[i]];
count_map.Remove(arr[i]);
count_map.Add(arr[i], a+1);
}
else
count_map.Add(arr[i], 1);
}
//count_map[arr[i]]++;
for (int i = 0; i < n; i++) // if count of element == k ,then
// it is the required first element
{
if (count_map[arr[i]] == k)
{
return arr[i];
}
}
// no element occurs k times
return -1;
}
// Driver code
public static void Main(String[] args)
{
int []arr = {1, 7, 4, 3, 4, 8, 7};
int n = arr.Length;
int k = 2;
Console.WriteLine(firstElement(arr, n, k));
}
}
// This code has been contributed by 29AjayKumar
Javascript