Skip to content
Related Articles

Related Articles

Improve Article

Find array elements with frequencies in range [l , r]

  • Difficulty Level : Basic
  • Last Updated : 31 May, 2021

Given an array of integers, find the elements from the array whose frequency lies in the range [l, r].

Examples: 

Input : arr[] = { 1, 2, 3, 3, 2, 2, 5 }
        l = 2, r = 3
Output : 2 3 3 2 2

Approach :  

  • Take a hash map, which will store the frequency of all the elements in the array.
  • Now, traverse once again.
  • Print the elements whose frequency lies between the range [l, r].

C++




// C++ program to find the elements whose
// frequency lies in the range [l, r]
#include "iostream"
#include "unordered_map"
using namespace std;
 
void findElements(int arr[], int n, int l, int r)
{
    // Hash map which will store the
    // frequency of the elements of the array.
    unordered_map<int, int> mp;
 
    for (int i = 0; i < n; ++i) {
 
        // Increment the frequency
        // of the element by 1.
        mp[arr[i]]++;
    }
 
    for (int i = 0; i < n; ++i) {
 
        // Print the element whose frequency
        // lies in the range [l, r]
        if (l <= mp[arr[i]] && mp[arr[i] <= r]) {
            cout << arr[i] << " ";
        }
    }
}
 
int main()
{
    int arr[] = { 1, 2, 3, 3, 2, 2, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int l = 2, r = 3;
    findElements(arr, n, l, r);
    return 0;
}

Java




import java.util.HashMap;
import java.util.Map;
 
// Java program to find the elements whose
// frequency lies in the range [l, r]
public class GFG {
 
    static void findElements(int arr[], int n, int l, int r) {
        // Hash map which will store the
        // frequency of the elements of the array.
        Map<Integer, Integer> mp = new HashMap<Integer, Integer>();
 
        for (int i = 0; i < n; ++i) {
 
            // Increment the frequency
            // of the element by 1.
            int a=0;
            if(mp.get(arr[i])==null){
                a=1;
            }
            else{
                a = mp.get(arr[i])+1;
            }
            mp.put(arr[i], a);
        }
 
        for (int i = 0; i < n; ++i) {
 
            // Print the element whose frequency
            // lies in the range [l, r]
            if (l <= mp.get(arr[i]) && (mp.get(arr[i]) <= r)) {
                System.out.print(arr[i] + " ");
            }
        }
    }
 
    public static void main(String[] args) {
        int arr[] = {1, 2, 3, 3, 2, 2, 5};
        int n = arr.length;
        int l = 2, r = 3;
        findElements(arr, n, l, r);
 
    }
}
/*This code is contributed by PrinciRaj1992*/

Python3




# Python 3 program to find the elements whose
# frequency lies in the range [l, r]
 
def findElements(arr, n, l, r):
     
    # Hash map which will store the
    # frequency of the elements of the array.
    mp = {i:0 for i in range(len(arr))}
 
    for i in range(n):
         
        # Increment the frequency
        # of the element by 1.
        mp[arr[i]] += 1
 
    for i in range(n):
         
        # Print the element whose frequency
        # lies in the range [l, r]
        if (l <= mp[arr[i]] and mp[arr[i] <= r]):
            print(arr[i], end = " ")
     
# Driver Code
if __name__ == '__main__':
    arr = [1, 2, 3, 3, 2, 2, 5]
    n = len(arr)
    l = 2
    r = 3
    findElements(arr, n, l, r)
     
# This code is contributed by
# Shashank_Sharma

C#




// C# program to find the elements whose
// frequency lies in the range [l, r]
using System;
using System.Collections.Generic;
 
class GFG
{
 
    static void findElements(int []arr, int n, int l, int r)
    {
        // Hash map which will store the
        // frequency of the elements of the array.
        Dictionary<int, int> mp = new Dictionary<int, int>();
 
        for (int i = 0; i < n; ++i)
        {
 
            // Increment the frequency
            // of the element by 1.
            int a = 0;
            if(!mp.ContainsKey(arr[i]))
            {
                a = 1;
            }
            else
            {
                a = mp[arr[i]]+1;
            }
             
            if(!mp.ContainsKey(arr[i]))
            {
                mp.Add(arr[i], a);
            }
            else
            {
                mp.Remove(arr[i]);
                mp.Add(arr[i], a);
            }
        }
 
        for (int i = 0; i < n; ++i)
        {
 
            // Print the element whose frequency
            // lies in the range [l, r]
            if (mp.ContainsKey(arr[i]) &&
                        l <= mp[arr[i]] &&
                        (mp[arr[i]] <= r))
            {
                Console.Write(arr[i] + " ");
            }
        }
    }
     
    // Driver code
    public static void Main(String[] args)
    {
        int []arr = {1, 2, 3, 3, 2, 2, 5};
        int n = arr.Length;
        int l = 2, r = 3;
        findElements(arr, n, l, r);
 
    }
}
 
// This code has been contributed by 29AjayKumar

Javascript




<script>
 
// Javascript program to find the elements whose
// frequency lies in the range [l, r]
function findElements(arr, n, l, r)
{
     
    // Hash map which will store the
    // frequency of the elements of the array.
    let mp = new Map();
 
    for(let i = 0; i < n; ++i)
    {
         
        // Increment the frequency
        // of the element by 1.
        let a = 0;
        if (mp.get(arr[i]) == null)
        {
            a = 1;
        }
        else
        {
            a = mp.get(arr[i]) + 1;
        }
        mp.set(arr[i], a);
    }
 
    for(let i = 0; i < n; ++i)
    {
         
        // Print the element whose frequency
        // lies in the range [l, r]
        if (l <= mp.get(arr[i]) &&
                (mp.get(arr[i]) <= r))
        {
            document.write(arr[i] + " ");
        }
    }
}
     
// Driver Code
let arr = [ 1, 2, 3, 3, 2, 2, 5 ];
let n = arr.length;
let l = 2, r = 3;
 
findElements(arr, n, l, r);
 
// This code is contributed by code_hunt
 
</script>
Output: 
2 3 3 2 2

 

Time Complexity – O(N)
 

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
Recommended Articles
Page :