Friday, January 10, 2025
Google search engine
HomeData Modelling & AIRemove elements from the array whose frequency lies in the range

Remove elements from the array whose frequency lies in the range [l, r]

Given an array of integers, remove 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 : 1 5 
We remove all those elements with frequencies from 2 to 5.

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

Approach : 

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

Implementation:

C++




// C++ program to remove the elements whose
// frequency appears in the range [l, r]
#include "iostream"
#include "unordered_map"
using namespace std;
 
void removeElements(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) {
 
        // Incrementing the frequency
        // of the element by 1.
        mp[arr[i]]++;
    }
 
    for (int i = 0; i < n; ++i) {
 
        // Print the element whose frequency
        // is not in the range [l, r]
        if (mp[arr[i]] < l or 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;
    removeElements(arr, n, l, r);
    return 0;
}


Java




// Java program to remove the elements whose
// frequency appears in the range [l, r]
import java.util.HashMap;
 
class GFG {
 
    static void removeElements(int arr[], int n, int l, int r)
    {
        // Hash map which will store the
        // frequency of the elements of the array.
        HashMap<Integer, Integer> mp = new HashMap<>();
 
        for (int i = 0; i < n; ++i) {
 
            // Incrementing the frequency
            // of the element by 1.
            // mp[arr[i]]++;
            int val = 0;
            if (mp.get(arr[i]) == null) {
                val = 1;
            }
            else {
                val = mp.get(arr[i]) + 1;
            }
            // System.out.println("--"+mp.get(arr[i]));
            mp.put(arr[i], val);
        }
 
        for (int i = 0; i < n; ++i) {
 
            // Print the element whose frequency
            // is not in the range [l, r]
            if (mp.get(arr[i]) < l || mp.get(arr[i]) > r) {
                System.out.print(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;
        removeElements(arr, n, l, r);
    }
}
 
// This code is contributed by 29AjayKumar


Python3




# Python 3 program to remove the elements
# whose frequency appears in the range [l, r]
 
def removeElements(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):
         
        # Incrementing the frequency
        # of the element by 1.
        mp[arr[i]] += 1
 
    for i in range(n):
         
        # Print the element whose frequency
        # is not in the range [l, r]
        if (mp[arr[i]] < l or 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
    removeElements(arr, n, l, r);
     
# This code is contributed by
# Sahil_Shelangia


C#




// C# program to remove the elements whose
// frequency appears in the range [l, r]
using System;
using System.Collections.Generic;
 
class GFG {
 
    static void removeElements(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) {
 
            // Incrementing the frequency
            // of the element by 1.
            // mp[arr[i]]++;
            int val = 0;
            if (!mp.ContainsKey(arr[i])) {
                val = 1;
            }
            else {
                val = mp[arr[i]] + 1;
            }
            if (!mp.ContainsKey(arr[i]))
                mp.Add(arr[i], val);
            else {
                mp.Remove(arr[i]);
                mp.Add(arr[i], val);
            }
        }
 
        for (int i = 0; i < n; ++i) {
 
            // Print the element whose frequency
            // is not in the range [l, r]
            if (mp[arr[i]] < l || 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;
        removeElements(arr, n, l, r);
    }
}
 
// This code contributed by Rajput-Ji


Javascript




<script>
 
// JavaScript program to remove the elements whose
// frequency appears in the range [l, r]
function removeElements(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)
    {
         
        // Incrementing the frequency
        // of the element by 1.
        // mp[arr[i]]++;
        let val = 0;
        if (mp.get(arr[i]) == null)
        {
            val = 1;
        }
        else
        {
            val = mp.get(arr[i]) + 1;
        }
        mp.set(arr[i], val);
    }
 
    for(let i = 0; i < n; ++i)
    {
         
        // Print the element whose frequency
        // is not in the range [l, r]
        if (mp.get(arr[i]) < l || 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;
 
removeElements(arr, n, l, r);
 
// This code is contributed by code_hunt
 
</script>


Output

1 5 

Time Complexity: O(N)
Auxiliary Space: O(N)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments