First element occurring k times in an array
Last Updated :
06 Sep, 2023
Given an array of n integers. The task is to find the first element that occurs k number of times. If no element occurs k times the print -1. The distribution of integer elements could be in any range.
Note : If multiple element occurs K number of time. then the element which occurs first will be our answer.
Examples:
Input: {1, 7, 4, 3, 4, 8, 7}, k = 2
Output: 7
Explanation: 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
Naive Approach: The idea is to use two nested loops. one for the selection of element and other for counting the number of time the selected element occurs in the given array.
Algorithm:
- Define a function firstElement that takes an integer array arr, an integer n representing the size of the array, and an integer k representing the number of times an element must occur in the array.
- Iterate through each element in the array arr using a for loop with index i.
- For each element arr[i], count how many times it occurs in the array by iterating through the array again using another for loop with index j.
- If the count of the current element arr[i] is equal to k, return the element arr[i].
- If no element occurs k times in the array, return -1.
- In the main function, initialize an integer array arr, its size n, and the value of k.
- Call the firstElement function with arr, n, and k as arguments, and print the returned value.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int firstElement( int arr[], int n, int k)
{
for ( int i = 0; i < n; i++) {
int count = 0;
for ( int j = 0; j < n; j++) {
if (arr[i] == arr[j])
count++;
}
if (count == k)
return arr[i];
}
return -1;
}
int main()
{
int arr[] = { 1, 7, 4, 3, 4, 8, 7 };
int n = sizeof (arr) / sizeof (arr[0]);
int k = 2;
cout << firstElement(arr, n, k);
return 0;
}
|
Java
public class GFG {
public static int firstElement( int [] arr, int n, int k)
{
for ( int i = 0 ; i < n; i++) {
int count = 0 ;
for ( int j = 0 ; j < n; j++) {
if (arr[i] == arr[j]) {
count++;
}
}
if (count == k) {
return arr[i];
}
}
return - 1 ;
}
public static void main(String[] args)
{
int [] arr = { 1 , 7 , 4 , 3 , 4 , 8 , 7 };
int n = arr.length;
int k = 2 ;
System.out.print(firstElement(arr, n, k));
}
}
|
Python3
def firstElement(arr, n, k):
for i in arr:
count = 0
for j in arr:
if i = = j:
count = count + 1
if count = = k:
return i
return - 1
if __name__ = = "__main__" :
arr = [ 1 , 7 , 4 , 3 , 4 , 8 , 7 ];
n = len (arr)
k = 2
print (firstElement(arr, n, k))
|
C#
using System;
public class GFG {
public static int firstElement( int [] arr, int n, int k)
{
for ( int i = 0; i < n; i++) {
int count = 0;
for ( int j = 0; j < n; j++) {
if (arr[i] == arr[j]) {
count++;
}
}
if (count == k) {
return arr[i];
}
}
return -1;
}
public static void Main(String[] args)
{
int [] arr = { 1, 7, 4, 3, 4, 8, 7 };
int n = arr.Length;
int k = 2;
Console.Write(firstElement(arr, n, k));
}
}
|
Javascript
class GFG
{
static firstElement(arr, n, k)
{
for ( var i = 0; i < n; i++)
{
var count = 0;
for ( var j=0; j < n; j++)
{
if (arr[i] == arr[j])
{
count++;
}
}
if (count == k)
{
return arr[i];
}
}
return -1;
}
static main(args)
{
var arr = [1, 7, 4, 3, 4, 8, 7];
var n = arr.length;
var k = 2;
console.log(GFG.firstElement(arr, n, k));
}
}
GFG.main([]);
|
Time complexity: O(n2).
Auxiliary space: O(1) as it is using constant space for variables
Efficient Approach: Use unordered_map for hashing as the range is not known. Steps:
- Traverse the array of elements from left to right.
- While traversing increment their count in the hash table.
- Again traverse the array from left to right and check which element has a count equal to k. Print that element and stop.
- If no element has a count equal to k, print -1.
Below is a dry run of the above approach:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int firstElement( int arr[], int n, int k)
{
unordered_map< int , int > count_map;
for ( int i=0; i<n; i++)
count_map[arr[i]]++;
for ( int i=0; i<n; i++)
if (count_map[arr[i]] == k)
return arr[i];
return -1;
}
int main()
{
int arr[] = {1, 7, 4, 3, 4, 8, 7};
int n = sizeof (arr) / sizeof (arr[0]);
int k = 2;
cout << firstElement(arr, n, k);
return 0;
}
|
Java
import java.util.HashMap;
class GFG {
static int firstElement( int arr[], int n, int k) {
HashMap<Integer, Integer> 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 );
}
for ( int i = 0 ; i < n; i++)
{
if (count_map.get(arr[i]) == k) {
return arr[i];
}
}
return - 1 ;
}
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));
}
}
|
Python3
def firstElement(arr, n, k):
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_map[arr[i]] = = k):
return arr[i]
i + = 1
return - 1
if __name__ = = "__main__" :
arr = [ 1 , 7 , 4 , 3 , 4 , 8 , 7 ];
n = len (arr)
k = 2
print (firstElement(arr, n, k))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int firstElement( int []arr, int n, int k)
{
Dictionary< int , int > count_map = new Dictionary< int , int >();
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);
}
for ( int i = 0; i < n; i++)
{
if (count_map[arr[i]] == k)
{
return arr[i];
}
}
return -1;
}
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));
}
}
|
Javascript
<script>
function firstElement(arr, n, k)
{
count_map = new Map()
for (let i=0; i<n; i++)
count_map[arr[i]] = 0;
for (let i=0; i<n; i++)
count_map[arr[i]]++;
for (let i=0; i<n; i++)
if (count_map[arr[i]] == k)
return arr[i];
return -1;
}
let arr = [1, 7, 4, 3, 4, 8, 7];
let n = arr.length;
let k = 2;
document.write(firstElement(arr, n, k));
<script>
|
Time Complexity: O(n)
Auxiliary Space: O(n) because we are using an auxiliary array of size n to store the count
Share your thoughts in the comments
Please Login to comment...