Related Articles

# Smallest element repeated exactly ‘k’ times (not limited to small range)

• Difficulty Level : Basic
• Last Updated : 22 Jun, 2021

Given an array of size n, the goal is to find out the smallest number that is repeated exactly ‘k’ times where k > 0?
And
Examples:

```Input : a[] = {2, 1, 3, 1, 2, 2}
k = 3
Output : 2

Input : a[] = {3, 4, 3, 2, 1, 5, 5}
k = 2
Output : 3
Explanation: As 3 is smaller than 5.
So 3 should be printed.```

We have discussed different solutions of this problem in below post.
Smallest element in an array that is repeated exactly ‘k’ times
The solutions discussed above are either limited to small range work in more than linear time. In this post a hashing based solution is discussed that works in O(n) time and is applicable to any range. Below are abstract steps.
1) Create a hash map that stores elements and their frequencies.
2) Traverse given array. For every element being traversed, increment its frequency.
3) Traverse hash map and print the smallest element with frequency k.

## C++

 `// C++ program to find the smallest element``// with frequency exactly k.``#include ``using` `namespace` `std;` `int` `smallestKFreq(``int` `a[], ``int` `n, ``int` `k)``{``    ``unordered_map<``int``, ``int``> m;` `    ``// Map is used to store the count of``    ``// elements present in the array``    ``for` `(``int` `i = 0; i < n; i++)``        ``m[a[i]]++;` `    ``// Traverse the map and find minimum``    ``// element with frequency k.``    ``int` `res = INT_MAX;``    ``for` `(``auto` `it = m.begin(); it != m.end(); ++it)``        ``if` `(it->second == k)``           ``res = min(res, it->first);` `    ``return` `(res != INT_MAX)? res : -1;``}` `// Driver code``int` `main()``{``    ``int` `arr[] = { 2, 2, 1, 3, 1 };``    ``int` `k = 2;``    ``int` `n = ``sizeof``(arr) / (``sizeof``(arr[0]));``    ``cout << smallestKFreq(arr, n, k);``    ``return` `0;``}`

## Java

 `// Java program to find the smallest element``// with frequency exactly k.``import` `java.util.*;` `class` `GFG {``     ` `    ``public` `static` `int` `smallestKFreq(``int` `a[], ``int` `n, ``int` `k)``    ``{``        ``HashMap m = ``new` `HashMap();``     ` `        ``// Map is used to store the count of``        ``// elements present in the array``        ``for` `(``int` `i = ``0``; i < n; i ++)``            ` `            ``if` `(m.containsKey(a[i]))``                ``m.put(a[i], m.get(a[i]) + ``1``);``        ` `           ``else` `m.put(a[i], ``1``);``     ` `        ``// Traverse the map and find minimum``        ``// element with frequency k.``        ``int` `res = Integer.MAX_VALUE;``        ``Set s = m.keySet();``        ` `        ``for` `(``int` `temp : s)``            ``if` `(m.get(temp) == k)``               ``res = Math.min(res, temp);``     ` `        ``return` `(res != Integer.MAX_VALUE)? res : -``1``;``    ``}``    ` `    ``/* Driver program to test above function */``    ``public` `static` `void` `main(String[] args)``    ``{``        ``int` `arr[] = { ``2``, ``2``, ``1``, ``3``, ``1` `};``        ``int` `k = ``2``;``        ` `        ``System.out.println(smallestKFreq(arr, arr.length, k));``     ` `    ``}``  ``}``// This code is contributed by Arnav Kr. Mandal.`

## Python3

 `from` `collections ``import` `defaultdict``import` `sys` `# Python program to find the smallest element``# with frequency exactly k.``def` `smallestKFreq(arr, n, k):``    ``mp ``=` `defaultdict(``lambda` `: ``0``)`  `    ``# Map is used to store the count of``    ``# elements present in the array``    ``for` `i ``in` `range``(n):``        ``mp[arr[i]] ``+``=` `1` `    ``# Traverse the map and find minimum``    ``# element with frequency k.``    ``res ``=` `sys.maxsize``    ``res1 ``=` `sys.maxsize` `    ``for` `key,values ``in` `mp.items():` `        ``if` `values ``=``=` `k:``            ``res ``=` `min``(res, key)``    ``return` `res ``if` `res !``=` `res1 ``else` `-``1` `# Driver code``arr ``=` `[``2``, ``2``, ``1``, ``3``, ``1``]``k ``=` `2``n ``=` `len``(arr)``print``(smallestKFreq(arr, n, k))` `# This code is contributed by Shrikant13`

## C#

 `// C# program to find the smallest element``// with frequency exactly k.``using` `System;``using` `System.Linq;``using` `System.Collections.Generic;``    ` `class` `GFG``{``    ` `    ``public` `static` `int` `smallestKFreq(``int` `[]a, ``int` `n, ``int` `k)``    ``{``        ``Dictionary<``int``,``int``> m = ``new` `Dictionary<``int``,``int``>();``    ` `        ``// Map is used to store the count of``        ``// elements present in the array``        ``for` `(``int` `i = 0; i < n; i ++)``            ` `            ``if` `(m.ContainsKey(a[i]))``            ``{``                ``var` `v = m[a[i]];``                ``m.Remove(a[i]);``                ``m.Add(a[i],v + 1);``            ``}``        ``else` `m.Add(a[i], 1);``    ` `        ``// Traverse the map and find minimum``        ``// element with frequency k.``        ``int` `res = ``int``.MaxValue;``        ``HashSet<``int``> s = ``new` `HashSet<``int``>(m.Keys.ToArray());``        ` `        ``foreach` `(``int` `temp ``in` `s)``            ``if` `(m[temp] == k)``            ``res = Math.Min(res, temp);``    ` `        ``return` `(res != ``int``.MaxValue)? res : -1;``    ``}``    ` `    ``/* Driver code */``    ``public` `static` `void` `Main(String[] args)``    ``{``        ``int` `[]arr = { 2, 2, 1, 3, 1 };``        ``int` `k = 2;``        ` `        ``Console.WriteLine(smallestKFreq(arr, arr.Length, k));``    ` `    ``}``}` `// This code is contributed by Rajput-Ji`

## Javascript

 ``

Output:

`1`

Time Complexity : O(n)
Auxiliary Space : O(n)
Related Article:
Smallest number repeating k times
This article is contributed by Gitanjali Sharma. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up