Remove the occurences of an element from array if it occurs more than n times I wanted to remove the occurrence of an element from an array if it occurs more than n times. For example:

{ 20, 37, 20, 21 } (n-&gt;1) -&gt; { 20, 37, 21}
{ 1, 1, 1, 1, 1 }  (n-&gt;5) -&gt; { 1, 1, 1, 1, 1 }
{ 1, 2, 3, 1, 1, 2, 1, 2, 3, 3, 2, 4, 5, 3, 1 } (n-&gt;3 ) -&gt; { 1, 2, 3, 1, 1, 2, 2, 3, 3, 4, 5 }


I tried the following code in java:

public class arr{
    public static void main(String[] args){
        int a[] = {1,1,3,3,7,2,2,2,2}; //Array of the elements
        int b =3; // maximum occurrences
        int u=1;  // Counter for same number of elements
        int c[] = new int[nterms(a, b)]; //initializing new array
        int k=0; 
        int l=0;
        for(int i =a.length-1; i&gt;=0; i--){
            k=0;
            // Checking the duplicate elements.
            for(int j=i-1; j&gt;=0; j--){
                if(a[i]==a[j]){
                    k++;    
                }
            }
            if(k!=b){ 
                c[c.length-u] = a[i];
                u++;
            }
        }
        for(int i =0; i&lt;c.length; i++)
            System.out.println(c[i]);  
    }

    // Returns no. of unique elements.
    public static int nterms(int[] x, int y){
        int k=0;
        int l=0;
        for(int i =0; i&lt;x.length; i++){
            k=0;
            for(int j=i+1; j&lt;x.length; j++){
                if(x[i]==x[j]){
                    k++;    
                }
            }
            if(k==y){
                l++;
            }
        }
        return (x.length-l);
    }
}


It works fine but can anybody recommend a more short &amp; efficient way.
It's been said multiple times but you should avoid one letter variable most of the time.

Class name arr is also no good. It's recommended that class name follows camel case convention and using abbreviation is often considered bad.  

Don't code something in your main as it makes it harder to test your code.
Putting it in an outside function makes for a clearer code :

public static int[] removeOccurencesFromArray(final int[] array, final long maxOccurence) {
    // TODO
}


Array have an "old-school" feel and I'd avoid them if possible. Consider using List (and especially ArrayList) for those kind of use case, thus transforming previous method in :

public static List&lt;Integer&gt; removeOccurencesFromList(final List&lt;Integer&gt; list, final long maxOccurence) {
    // TODO
}




Now, onto the main course :

I find your algorithm overly complex. :/
It's also higly "procedural" IMO, in modern computing we'd rather "tell" the program to do something instead of manipulating everything by ourselves.

Julien Rousé has shown a nice and short solution in another answer... which is probably faster than mine but please bear with me :)

(After rereading my answer, I see that I used some Java 8 tricks that may feel overwhelming if you are a beginner, if you are new to programming I'd recommend to not consider my solution)

Let's try to solve your problem step by step :  

Here is a simple solution I came up with (almost the same as Julien Rousé)
1) count the number of occurences within your array... you end up with a structure linking all elements with the number of occurence... it's usually called a bag, while there are some java library that supports this structure let's keep things simple and use a Map
2) if any occurences count is greater than the maxOccurence parameter then it should be set to maxOccurence
3) for each elements in your array, print it if the number of occurences is greater than 0 and reduce the number of elements in your bag

Let's dig a bit in java 8... surely there's a tool for us in there.
We are looking for something that'd allow us to "tell" instead of creating everything by hand.

In the Collectors util class (https://docs.oracle.com/javase/8/docs/api/java/util/stream/Collectors.html), we can find some really fancy tools. For our use case, groupingBy and counting sounds useful.

groupingBy allows you to transform a stream of data into a map where the key is found using the first parameter.
counting counts the number of input elements (from Javadoc).

list.stream().collect(Collectors.groupingBy(element -&gt; element, Collectors.counting()));


With this simple line, we now have the number of occurence of each elements of your list.

This is still not enough as we want to consider the maximum number of occurences.

We want to have the number of occurence if it's below a threshold.
Collectors (again) comes to the rescue :

list.stream().collect(Collectors.groupingBy(element -&gt; element,
            Collectors.collectingAndThen(Collectors.counting(), v -&gt; Math.min(v, maxOccurence))));


So we are counting unless we have more than maxOccurence, in which case the min will always give us maxOccurence as a result.
That's not very palatable though IMO, I'll use static import to make it cleaner. Let's also not forget to store the result.

    Map&lt;Integer, Long&gt; value = list.stream().collect(groupingBy(element -&gt; element,
            collectingAndThen(counting(), v -&gt; Math.min(v, maxOccurence))));


We can now easily compute the result :

    List&lt;Integer&gt; result = new ArrayList&lt;&gt;();
    for (Integer element : list) {
        long remaining = value.get(element);
        if (remaining &gt; 0) {
            value.put(element, remaining - 1);
            result.add(element);
        }
    }

    return result;


If you really want to have an array at the end, use the toArray method ;)