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++
// C++ program to find smallest number
// in array that is repeated exactly
// 'k' times.
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1000;
int findDuplicate(int arr[], int n, int k)
{
// Since arr[] has numbers in range from
// 1 to MAX
int res = MAX + 1;
for (int i = 0; i < n; i++) {
if (arr[i] > 0) {
// set count to 1 as number is present
// once
int count = 1;
for (int j = i + 1; j < n; j++)
if (arr[i] == arr[j])
count += 1;
// If frequency of number is equal to 'k'
if (count == k)
res = min(res, arr[i]);
}
}
return res;
}
// Driver code
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
// Java program to find smallest number
// in array that is repeated exactly
// 'k' times.
public class GFG {
static final int MAX = 1000;
// finds the smallest number in arr[]
// that is repeated k times
static int findDuplicate(int arr[], int n, int k)
{
// Since arr[] has numbers in range from
// 1 to MAX
int res = MAX + 1;
for (int i = 0; i < n; i++) {
if (arr[i] > 0) {
// set count to 1 as number is
// present once
int count = 1;
for (int j = i + 1; j < n; j++)
if (arr[i] == arr[j])
count += 1;
// If frequency of number is equal
// to 'k'
if (count == k)
res = Math.min(res, arr[i]);
}
}
return res;
}
// Driver code
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));
}
}
// This article is contributed by Sumit Ghosh
Python3
# Python 3 program to find smallest
# number in array that is repeated
# exactly 'k' times.
MAX = 1000
def findDuplicate(arr, n, k):
# Since arr[] has numbers in
# range from 1 to MAX
res = MAX + 1
for i in range(0, n):
if (arr[i] > 0):
# set count to 1 as number
# is present once
count = 1
for j in range(i + 1, n):
if (arr[i] == arr[j]):
count += 1
# If frequency of number is equal to 'k'
if (count == k):
res = min(res, arr[i])
return res
# Driver code
arr = [2, 2, 1, 3, 1]
k = 2
n = len(arr)
print(findDuplicate(arr, n, k))
# This code is contributed by Smitha Dinesh Semwal.
C#
// C# program to find smallest number
// in array that is repeated exactly
// 'k' times.
using System;
public class GFG {
static int MAX = 1000;
// finds the smallest number in arr[]
// that is repeated k times
static int findDuplicate(int[] arr,
int n, int k)
{
// Since arr[] has numbers in range
// from 1 to MAX
int res = MAX + 1;
for (int i = 0; i < n; i++)
{
if (arr[i] > 0)
{
// set count to 1 as number
// is present once
int count = 1;
for (int j = i + 1; j < n; j++)
if (arr[i] == arr[j])
count += 1;
// If frequency of number is
// equal to 'k'
if (count == k)
res = Math.Min(res, arr[i]);
}
}
return res;
}
// Driver code
public static void Main()
{
int[] arr = { 2, 2, 1, 3, 1 };
int k = 2;
int n = arr.Length;
Console.WriteLine(
findDuplicate(arr, n, k));
}
}
// This article is contributed by vt_m.
PHP
<?php
// PHP program to find smallest number
// in array that is repeated exactly
// 'k' times.
function findDuplicate($arr, $n, $k)
{
// Since arr[] has numbers in
// range from 1 to MAX
$MAX = 1000;
$res = $MAX + 1;
for ($i = 0; $i < $n; $i++)
{
if ($arr[$i] > 0)
{
// set count to 1 as number is
// present once
$count = 1;
for ($j = $i + 1; $j < $n; $j++)
if ($arr[$i] == $arr[$j])
$count += 1;
// If frequency of number is
// equal to 'k'
if ($count == $k)
$res = min($res, $arr[$i]);
}
}
return $res;
}
// Driver code
$arr = array(2, 2, 1, 3, 1);
$k = 2;
$n = count($arr);
echo findDuplicate($arr, $n, $k);
// This code is contributed by Rajput-Ji
?>
JavaScript
<script>
// Javascript program to find smallest number
// in array that is repeated exactly
// 'k' times
let MAX = 1000;
// finds the smallest number in arr[]
// that is repeated k times
function findDuplicate(arr, n, k)
{
// Computing frequencies of all elements
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;
}
// Finding the smallest element with
// frequency as k
for (let i = 0; i < MAX; i++) {
// If frequency of any of the number
// is equal to k starting from 0
// then return the number
if (freq[i] == k)
return i;
}
return -1;
}
// driver program
let arr = [ 2, 2, 1, 3, 1 ];
let k = 2;
let n = arr.length;
document.write(findDuplicate(arr, n, k));
// This code is contributed by code_hunt.
</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++
// C++ program to find smallest number
// in array that is repeated exactly
// 'k' times.
#include <bits/stdc++.h>
using namespace std;
int findDuplicate(int arr[], int n, int k)
{
// Sort the array
sort(arr, arr + n);
// Find the first element with exactly
// k occurrences.
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;
}
// Driver code
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
// Java program to find smallest number
// in array that is repeated exactly
// 'k' times.
import java.util.Arrays;
public class GFG {
// finds the smallest number in arr[]
// that is repeated k times
static int findDuplicate(int arr[], int n, int k)
{
// Sort the array
Arrays.sort(arr);
// Find the first element with exactly
// k occurrences.
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;
}
// Driver code
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));
}
}
// This article is contributed by Sumit Ghosh
Python3
# Python program to find smallest number
# in array that is repeated exactly
# 'k' times.
def findDuplicate(arr, n, k):
# Sort the array
arr.sort()
# Find the first element with exactly
# k occurrences.
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
# Driver code
arr = [ 2, 2, 1, 3, 1 ];
k = 2
n = len(arr)
print(findDuplicate(arr, n, k))
# This code is contributed by Sachin Bisht
C#
// C# program to find smallest number
// in array that is repeated exactly
// 'k' times.
using System;
public class GFG {
// finds the smallest number in arr[]
// that is repeated k times
static int findDuplicate(int[] arr,
int n, int k)
{
// Sort the array
Array.Sort(arr);
// Find the first element with
// exactly k occurrences.
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;
}
// Driver code
public static void Main()
{
int[] arr = { 2, 2, 1, 3, 1 };
int k = 2;
int n = arr.Length;
Console.WriteLine(
findDuplicate(arr, n, k));
}
}
// This article is contributed by vt_m.
PHP
<?php
// PHP program to find smallest number
// in array that is repeated exactly
// 'k' times.
// finds the smallest number in arr[]
// that is repeated k times
function findDuplicate($arr, $n, $k)
{
// Sort the array
sort($arr);
// Find the first element with
// exactly k occurrences.
$i = 0;
while ($i < $n)
{
$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;
}
// Driver code
$arr = array( 2, 2, 1, 3, 1 );
$k = 2;
$n = sizeof($arr);
echo(findDuplicate($arr, $n, $k));
// This code is contributed
// by Code_Mech.
?>
JavaScript
<script>
// JavaScript program to find smallest number
// in array that is repeated exactly
// 'k' times.
// finds the smallest number in arr[]
// that is repeated k times
function findDuplicate(arr, n, k)
{
// Sort the array
arr.sort();
// Find the first element with
// exactly k occurrences.
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++
// C++ program to find smallest number
// in array that is repeated exactly
// 'k' times.
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1000;
int findDuplicate(int arr[], int n, int k)
{
// Computing frequencies of all elements
int freq[MAX];
memset(freq, 0, sizeof(freq));
for (int i = 0; i < n; i++) {
if (arr[i] < 1 && arr[i] > MAX) {
cout << "Out of range";
return -1;
}
freq[arr[i]] += 1;
}
// Finding the smallest element with
// frequency as k
for (int i = 0; i < MAX; i++) {
// If frequency of any of the number
// is equal to k starting from 0
// then return the number
if (freq[i] == k)
return i;
}
return -1;
}
// Driver code
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
// Java program to find smallest number
// in array that is repeated exactly
// 'k' times.
public class GFG {
static final int MAX = 1000;
// finds the smallest number in arr[]
// that is repeated k times
static int findDuplicate(int arr[], int n, int k)
{
// Computing frequencies of all elements
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;
}
// Finding the smallest element with
// frequency as k
for (int i = 0; i < MAX; i++) {
// If frequency of any of the number
// is equal to k starting from 0
// then return the number
if (freq[i] == k)
return i;
}
return -1;
}
// Driver code
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));
}
}
// This article is contributed by Sumit Ghosh
Python3
# Python program to find smallest number
# in array that is repeated exactly
# 'k' times.
MAX = 1000
def findDuplicate(arr, n, k):
# Computing frequencies of all elements
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
# Finding the smallest element with
# frequency as k
for i in range(MAX):
# If frequency of any of the number
# is equal to k starting from 0
# then return the number
if (freq[i] == k):
return i
return -1
# Driver code
arr = [ 2, 2, 1, 3, 1 ]
k = 2
n = len(arr)
print(findDuplicate(arr, n, k))
# This code is contributed by Sachin Bisht
C#
// C# program to find smallest number
// in array that is repeated exactly
// 'k' times.
using System;
public class GFG {
static int MAX = 1000;
// finds the smallest number in arr[]
// that is repeated k times
static int findDuplicate(int[] arr,
int n, int k)
{
// Computing frequencies of all
// elements
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;
}
// Finding the smallest element with
// frequency as k
for (int i = 0; i < MAX; i++) {
// If frequency of any of the
// number is equal to k starting
// from 0 then return the number
if (freq[i] == k)
return i;
}
return -1;
}
// Driver code
public static void Main()
{
int[] arr = { 2, 2, 1, 3, 1 };
int k = 2;
int n = arr.Length;
Console.WriteLine(
findDuplicate(arr, n, k));
}
}
// This article is contributed by vt_m.
PHP
<?php
// PHP program to find smallest number
// in array that is repeated exactly
// 'k' times.
$MAX = 1000;
function findDuplicate($arr, $n, $k)
{
global $MAX;
// Computing frequencies of all elements
$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;
}
// Finding the smallest element with
// frequency as k
for ($i = 0; $i < $MAX; $i++)
{
// If frequency of any of the number
// is equal to k starting from 0
// then return the number
if ($freq[$i] == $k)
return $i;
}
return -1;
}
// Driver code
$arr = array( 2, 2, 1, 3, 1 );
$k = 2;
$n = count($arr);
echo findDuplicate($arr, $n, $k);
// This code is contributed by mits
?>
JavaScript
<script>
// javascript program to find smallest number
// in array that is repeated exactly
// 'k' times.
var MAX = 1000;
// finds the smallest number in arr
// that is repeated k times
function findDuplicate(arr , n , k)
{
// Computing frequencies of all elements
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;
}
// Finding the smallest element with
// frequency as k
for (var i = 0; i < MAX; i++) {
// If frequency of any of the number
// is equal to k starting from 0
// then return the number
if (freq[i] == k)
return i;
}
return -1;
}
// Driver code
var arr = [ 2, 2, 1, 3, 1 ];
var k = 2;
var n = arr.length;
document.write(findDuplicate(arr, n, k));
// This code is contributed by 29AjayKumar
</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.
Similar Reads
DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on
7 min read
Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s
12 min read
Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge
14 min read
Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st
2 min read
Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir
8 min read
Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta
15+ min read
Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc
15 min read
Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T
9 min read
Dijkstra's Algorithm to find Shortest Paths from a Source to all Given a weighted undirected graph represented as an edge list and a source vertex src, find the shortest path distances from the source vertex to all other vertices in the graph. The graph contains V vertices, numbered from 0 to V - 1.Note: The given graph does not contain any negative edge. Example
12 min read
Selection Sort Selection Sort is a comparison-based sorting algorithm. It sorts an array by repeatedly selecting the smallest (or largest) element from the unsorted portion and swapping it with the first unsorted element. This process continues until the entire array is sorted.First we find the smallest element an
8 min read