Smallest element in an array that is repeated exactly ‘k’ times.
Last Updated :
18 Jul, 2022
Given an array of size n, the goal is to find out the smallest number that is repeated exactly ‘k’ times where k > 0?
Assume that array has only positive integers and 1 <= arr[i] < 1000 for each i = 0 to n -1.
Examples:
Input : arr[] = {2 2 1 3 1}
k = 2
Output: 1
Explanation:
Here in array,
2 is repeated 2 times
1 is repeated 2 times
3 is repeated 1 time
Hence 2 and 1 both are repeated 'k' times
i.e 2 and min(2, 1) is 1
Input : arr[] = {3 5 3 2}
k = 1
Output : 2
Explanation:
Both 2 and 5 are repeating 1 time but
min(5, 2) is 2
Simple Approach: A simple approach is to use two nested loops. The outer loop picks an element one by one starting from the leftmost element. The inner loop checks if the same element is present on right side of it. If present increase the count and make the number negative which we got on the right side to prevent it from counting again.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1000;
int findDuplicate( int arr[], int n, int k)
{
int res = MAX + 1;
for ( int i = 0; i < n; i++) {
if (arr[i] > 0) {
int count = 1;
for ( int j = i + 1; j < n; j++)
if (arr[i] == arr[j])
count += 1;
if (count == k)
res = min(res, arr[i]);
}
}
return res;
}
int main()
{
int arr[] = { 2, 2, 1, 3, 1 };
int k = 2;
int n = sizeof (arr) / ( sizeof (arr[0]));
cout << findDuplicate(arr, n, k);
return 0;
}
|
Java
Python3
MAX = 1000
def findDuplicate(arr, n, k):
res = MAX + 1
for i in range ( 0 , n):
if (arr[i] > 0 ):
count = 1
for j in range (i + 1 , n):
if (arr[i] = = arr[j]):
count + = 1
if (count = = k):
res = min (res, arr[i])
return res
arr = [ 2 , 2 , 1 , 3 , 1 ]
k = 2
n = len (arr)
print (findDuplicate(arr, n, k))
|
C#
using System;
public class GFG {
static int MAX = 1000;
static int findDuplicate( int [] arr,
int n, int k)
{
int res = MAX + 1;
for ( int i = 0; i < n; i++)
{
if (arr[i] > 0)
{
int count = 1;
for ( int j = i + 1; j < n; j++)
if (arr[i] == arr[j])
count += 1;
if (count == k)
res = Math.Min(res, arr[i]);
}
}
return res;
}
public static void Main()
{
int [] arr = { 2, 2, 1, 3, 1 };
int k = 2;
int n = arr.Length;
Console.WriteLine(
findDuplicate(arr, n, k));
}
}
|
PHP
Javascript
<script>
let MAX = 1000;
function findDuplicate(arr, n, k)
{
let freq = new Array(MAX).fill(0);
for (let i = 0; i < n; i++) {
if (arr[i] < 1 && arr[i] > MAX) {
document.write( "Out of range" );
return -1;
}
freq[arr[i]] += 1;
}
for (let i = 0; i < MAX; i++) {
if (freq[i] == k)
return i;
}
return -1;
}
let arr = [ 2, 2, 1, 3, 1 ];
let k = 2;
let n = arr.length;
document.write(findDuplicate(arr, n, k));
</script>
|
Time Complexity : O(n2)
Auxiliary Space : O(1)
This solution doesn’t require array elements to be in limited range.
Better Solution : Sort the input array and find the first element with exactly k count of appearances.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int findDuplicate( int arr[], int n, int k)
{
sort(arr, arr + n);
int i = 0;
while (i < n) {
int j, count = 1;
for (j = i + 1; j < n && arr[j] == arr[i]; j++)
count++;
if (count == k)
return arr[i];
i = j;
}
return -1;
}
int main()
{
int arr[] = { 2, 2, 1, 3, 1 };
int k = 2;
int n = sizeof (arr) / ( sizeof (arr[0]));
cout << findDuplicate(arr, n, k);
return 0;
}
|
Java
import java.util.Arrays;
public class GFG {
static int findDuplicate( int arr[], int n, int k)
{
Arrays.sort(arr);
int i = 0 ;
while (i < n) {
int j, count = 1 ;
for (j = i + 1 ; j < n && arr[j] == arr[i]; j++)
count++;
if (count == k)
return arr[i];
i = j;
}
return - 1 ;
}
public static void main(String[] args)
{
int arr[] = { 2 , 2 , 1 , 3 , 1 };
int k = 2 ;
int n = arr.length;
System.out.println(findDuplicate(arr, n, k));
}
}
|
Python3
def findDuplicate(arr, n, k):
arr.sort()
i = 0
while (i < n):
j, count = i + 1 , 1
while (j < n and arr[j] = = arr[i]):
count + = 1
j + = 1
if (count = = k):
return arr[i]
i = j
return - 1
arr = [ 2 , 2 , 1 , 3 , 1 ];
k = 2
n = len (arr)
print (findDuplicate(arr, n, k))
|
C#
using System;
public class GFG {
static int findDuplicate( int [] arr,
int n, int k)
{
Array.Sort(arr);
int i = 0;
while (i < n) {
int j, count = 1;
for (j = i + 1; j < n &&
arr[j] == arr[i]; j++)
count++;
if (count == k)
return arr[i];
i = j;
}
return -1;
}
public static void Main()
{
int [] arr = { 2, 2, 1, 3, 1 };
int k = 2;
int n = arr.Length;
Console.WriteLine(
findDuplicate(arr, n, k));
}
}
|
PHP
Javascript
<script>
function findDuplicate(arr, n, k)
{
arr.sort();
let i = 0;
while (i < n) {
let j, count = 1;
for (j = i + 1; j < n &&
arr[j] == arr[i]; j++)
count++;
if (count == k)
return arr[i];
i = j;
}
return -1;
}
let arr = [ 2, 2, 1, 3, 1 ];
let k = 2;
let n = arr.length;
document.write(findDuplicate(arr, n, k));
</script>
|
Time Complexity : O(n Log n)
Auxiliary Space : O(1)
Efficient Approach: Efficient approach is based on the fact that array has numbers in small range (1 to 1000). We solve this problem by using a frequency array of size max and store the frequency of every number in that array.
Implementation:
C++
Java
public class GFG {
static final int MAX = 1000 ;
static int findDuplicate( int arr[], int n, int k)
{
int [] freq = new int [MAX];
for ( int i = 0 ; i < n; i++) {
if (arr[i] < 1 && arr[i] > MAX) {
System.out.println( "Out of range" );
return - 1 ;
}
freq[arr[i]] += 1 ;
}
for ( int i = 0 ; i < MAX; i++) {
if (freq[i] == k)
return i;
}
return - 1 ;
}
public static void main(String[] args)
{
int arr[] = { 2 , 2 , 1 , 3 , 1 };
int k = 2 ;
int n = arr.length;
System.out.println(findDuplicate(arr, n, k));
}
}
|
Python3
MAX = 1000
def findDuplicate(arr, n, k):
freq = [ 0 for i in range ( MAX )]
for i in range (n):
if (arr[i] < 1 and arr[i] > MAX ):
print ( "Out of range" )
return - 1
freq[arr[i]] + = 1
for i in range ( MAX ):
if (freq[i] = = k):
return i
return - 1
arr = [ 2 , 2 , 1 , 3 , 1 ]
k = 2
n = len (arr)
print (findDuplicate(arr, n, k))
|
C#
using System;
public class GFG {
static int MAX = 1000;
static int findDuplicate( int [] arr,
int n, int k)
{
int [] freq = new int [MAX];
for ( int i = 0; i < n; i++)
{
if (arr[i] < 1 && arr[i] > MAX)
{
Console.WriteLine( "Out of range" );
return -1;
}
freq[arr[i]] += 1;
}
for ( int i = 0; i < MAX; i++) {
if (freq[i] == k)
return i;
}
return -1;
}
public static void Main()
{
int [] arr = { 2, 2, 1, 3, 1 };
int k = 2;
int n = arr.Length;
Console.WriteLine(
findDuplicate(arr, n, k));
}
}
|
PHP
<?php
$MAX = 1000;
function findDuplicate( $arr , $n , $k )
{
global $MAX ;
$freq = array_fill (0, $MAX , 0);
for ( $i = 0; $i < $n ; $i ++)
{
if ( $arr [ $i ] < 1 && $arr [ $i ] > $MAX )
{
echo "Out of range" ;
return -1;
}
$freq [ $arr [ $i ]] += 1;
}
for ( $i = 0; $i < $MAX ; $i ++)
{
if ( $freq [ $i ] == $k )
return $i ;
}
return -1;
}
$arr = array ( 2, 2, 1, 3, 1 );
$k = 2;
$n = count ( $arr );
echo findDuplicate( $arr , $n , $k );
?>
|
Javascript
<script>
var MAX = 1000;
function findDuplicate(arr , n , k)
{
var freq = Array.from({length: MAX}, (_, i) => 0);
for ( var i = 0; i < n; i++) {
if (arr[i] < 1 && arr[i] > MAX) {
document.write( "Out of range" );
return -1;
}
freq[arr[i]] += 1;
}
for ( var i = 0; i < MAX; i++) {
if (freq[i] == k)
return i;
}
return -1;
}
var arr = [ 2, 2, 1, 3, 1 ];
var k = 2;
var n = arr.length;
document.write(findDuplicate(arr, n, k));
</script>
|
Time Complexity: O(MAX + n)
Auxiliary Space : O(MAX)
Can we solve it in O(n) time if range is not limited?
Please see Smallest element repeated exactly ‘k’ times (not limited to small range)
This article is contributed by Abhijit Shankhdhar.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...