Given an array arr[] and an integer k, return the first element that appears at least k times; if none exists, return -1.
Examples:
Input: arr[] = [1, 7, 4, 3, 4, 8, 7], k = 2
Output: 4
Explanation: Both 7 and 4 occur 2 times. But 4 is first that occurs twice. At the index = 4, is the first element.Input: arr[] = [3, 1, 3, 4, 5, 1, 3, 3, 5, 4], k = 3
Output: 3
Explanation: Here, 3 is the only number that appeared atleast 3 times in the array.Input: arr[] = [10, 8, 2], k = 10
Output: -1
Explanation: Here no element is returning atleast 10 number of times, so -1.
Table of Content
[Naive Approach] Using Nested Loops – O(n²) Time and O(1) Space
To find the first element that occurs
ktimes, iterate through the array fromi = 0ton-1and count how many timesarr[i]has appeared in the range[0, i]using another loop. As soon as the count of any element becomes equal tok, return that element immediately since it is the first to reachkoccurrences. If no element reacheskoccurrences, return-1.
#include <iostream>
#include <vector>
using namespace std;
int firstElementKTime(vector<int> arr, int k)
{
int n = arr.size();
for (int i = 0; i < n; i++) {
int cnt = 0;
for (int j = 0; j <= i; j++) {
if (arr[j] == arr[i])
cnt++;
}
// If frequency becomes k at this point
if (cnt == k)
return arr[i];
}
return -1;
}
// Driver code
int main()
{
vector<int> arr = {1, 7, 4, 3, 4, 8, 7};
int k = 2;
cout << firstElementKTime(arr, k);
return 0;
}
import java.util.HashMap;
class GFG {
public static int firstElementKTime(int[] arr, int k) {
HashMap<Integer, Integer> map = new HashMap<>();
for (int num : arr) {
map.put(num, map.getOrDefault(num, 0) + 1);
if (map.get(num) == k) {
return num;
}
}
return -1;
}
public static void main(String[] args) {
int[] arr = {1, 7, 4, 3, 4, 8, 7};
int k = 2;
System.out.println(firstElementKTime(arr, k));
}
}
def firstElementKTime(arr, k):
freq = {}
for num in arr:
freq[num] = freq.get(num, 0) + 1
if freq[num] == k:
return num
return -1
# Driver code
arr = [1, 7, 4, 3, 4, 8, 7]
k = 2
print(firstElementKTime(arr, k))
using System;
using System.Collections.Generic;
class GFG {
public static int firstElementKTime(int[] arr, int k) {
Dictionary<int, int> map = new Dictionary<int, int>();
foreach (int num in arr) {
if (map.ContainsKey(num))
map[num]++;
else
map[num] = 1;
if (map[num] == k)
return num;
}
return -1;
}
static void Main() {
int[] arr = {1, 7, 4, 3, 4, 8, 7};
int k = 2;
Console.WriteLine(firstElementKTime(arr, k));
}
}
function firstElementKTime(arr, k) {
let freq = new Map();
for (let num of arr) {
freq.set(num, (freq.get(num) || 0) + 1);
if (freq.get(num) === k) {
return num;
}
}
return -1;
}
// Driver code
let arr = [1, 7, 4, 3, 4, 8, 7];
let k = 2;
console.log(firstElementKTime(arr, k));
Output
4
[Expected Approach] Using Hash Map – O(n) Time and O(n) Space
Traverse the array from
i = 0ton-1while maintaining a hash map to store the frequency of elements. As soon as its frequency of an element becomes equal tok, return that element immediately since it is the first to reachkoccurrences. If no element reacheskoccurrences, return-1.
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
int firstElementKTime(vector<int>& arr, int k) {
int n = arr.size();
unordered_map<int, int> mp;
for (int i = 0; i < n; i++) {
mp[arr[i]]++;
// if the frequency becomes equal to k, we return the element.
if (mp[arr[i]] == k)
return arr[i];
}
// if no element occurs k times, we return -1.
return -1;
}
// Driver code
int main()
{
vector<int> arr = {1, 7, 4, 3, 4, 8, 7};
int k = 2;
cout << firstElementKTime(arr, k);
return 0;
}
import java.util.HashMap;
class GFG {
public static int firstElementKTime(int[] arr, int k) {
int n = arr.length;
HashMap<Integer, Integer> mp = new HashMap<>();
for (int i = 0; i < n; i++) {
mp.put(arr[i], mp.getOrDefault(arr[i], 0) + 1);
// if the frequency becomes equal to k, we return the element.
if (mp.get(arr[i]) == k)
return arr[i];
}
// if no element occurs k times, we return -1.
return -1;
}
// Driver code
public static void main(String[] args) {
int[] arr = {1, 7, 4, 3, 4, 8, 7};
int k = 2;
System.out.println(firstElementKTime(arr, k));
}
}
def firstElementKTime(arr, k):
n = len(arr)
mp = {}
for i in range(n):
mp[arr[i]] = mp.get(arr[i], 0) + 1
# if the frequency becomes equal to k, we return the element.
if mp[arr[i]] == k:
return arr[i]
# if no element occurs k times, we return -1.
return -1
# Driver code
arr = [1, 7, 4, 3, 4, 8, 7]
k = 2
print(firstElementKTime(arr, k))
using System;
using System.Collections.Generic;
class GFG {
public static int firstElementKTime(int[] arr, int k) {
int n = arr.Length;
Dictionary<int, int> mp = new Dictionary<int, int>();
for (int i = 0; i < n; i++) {
if (mp.ContainsKey(arr[i]))
mp[arr[i]]++;
else
mp[arr[i]] = 1;
// if the frequency becomes equal to k, we return the element.
if (mp[arr[i]] == k)
return arr[i];
}
// if no element occurs k times, we return -1.
return -1;
}
// Driver code
static void Main() {
int[] arr = {1, 7, 4, 3, 4, 8, 7};
int k = 2;
Console.WriteLine(firstElementKTime(arr, k));
}
}
function firstElementKTime(arr, k) {
let n = arr.length;
let mp = new Map();
for (let i = 0; i < n; i++) {
mp.set(arr[i], (mp.get(arr[i]) || 0) + 1);
// if the frequency becomes equal to k, we return the element.
if (mp.get(arr[i]) === k)
return arr[i];
}
// if no element occurs k times, we return -1.
return -1;
}
// Driver code
let arr = [1, 7, 4, 3, 4, 8, 7];
let k = 2;
console.log(firstElementKTime(arr, k));
Output
4
Similar Problems: