Is there a way to copy int[] to another int[] without repeated ints?

Tags: ,



We can’t use ArrayList or something like that, because the teacher told us to not use them, so I got stuck at this point. The signature of the function is:

public static int[] deleteNth(int[] elements, int maxOccurrences){}

I already go down over the array and get the lenght for the copied int result[] that i will return but now i got stucked thinking how i can paste certain elements. I wrote the I/O of the method:

deleteNth(new int[] {20,37,20,21}, 1) // return [20,37,21]
deleteNth(new int[] {1,1,3,3,7,2,2,2,2}, 3) // return [1, 1, 3, 3, 7, 2, 2, 2]

In a last chance for me, i try something like this but my brains burn out

for(int n:result) {
 int n1=0;
 boolean nope=false;
 for(;n1<elements.length;) {
  //TODOthings
 }
 result[n]=elements[n1];
} 

For those people who don’t believe me, here’s my code:

public static int[] deleteNth(int[] elements, int maxOccurrences) {
        int[] result = null;
        if (elements != null && maxOccurrences > 0) {
            int result_lenght=elements.length;
            int ocurrences=0;
            for(int n:elements) {
                for(int n1:elements) {
                    if(n==n1 && ocurrences!=maxOccurrences) {
                        ocurrences++;
                        result_lenght--;
                    }
                }
            }
            result=new int[result_lenght];
            for(int n:result) {
                int n1=0;
                boolean nope=false;
                for(;n1<elements.length;) {
                    //todothings
                }
                result[n]=elements[n1];
            }
        }else {
            result=elements;
        }
        return result;
    }

Answer

A possible straightforward solution (albeit inefficient) would be to start by creating a method that accepts as parameter an array, the number of elements on it, and a value, and returns the number of times that the value appears in the array, something like:

int total_occurences(int[] elements, int size, int value) 

Use that method in your main code:

int copy[] = new int [elements.length];
int elements_copied = 0;
for(int i = 0; i < elements.length; i++) {
    // Check if you are "allowed" to copy the element
    if(total_occurences(copy, elements_copied , value <= maxOccurrences){
      copy[elements_copied] = element[i];
      elements_copied ++;
    }          
}
// copy the content to a new array of size = elements_copied
// return that array

The main idea is to first create an array (i.e., int copy[] = new int [elements.length]) with the same size as the array elements, since you do not beforehand how many duplicates there are. Iterate over the array elements, and for the current element (i.e., element[i]) check if we already have a copied (that element) the maximum number allowed (i.e., maxOccurrences):

if(total_occurences(copy, elements_copied , value <= maxOccurrences)

if not copy the element and increment the number of elements copied so far:

 copy[elements_copied] = element[i];
 elements_copied ++;

We need to use a different variable to iterate over the copy array because it might contain a different number of elements than the array elements. For instance, after a duplicated number has been “removed”. In the end, you can create a new array (the one to be returned) with a size equals to the variable elements_copied, and copy the elements from the copy array into this newly created array. In this way, you will return an array without missing values at the end of it.



Source: stackoverflow