Im trying to filter out any duplicate arrays that are passed through and return
ID: 3565741 • Letter: I
Question
Im trying to filter out any duplicate arrays that are passed through and return an array of only uniqe arrays that dont have any duplicates. Here is my code so far. (I am only allowed to use ARRAYS. I cant use Arraylist or LinkedList).
public Animal[] removeDuplicateAnimals(Animal[] animals) {
int i = 0;
Animal[] uniqueArray = new Animal[i];
while(i<animals.length){
for(int j=0; j<animals.length; j++){
for(int k=0; k<animals.length; k++){
if(k!=j && animals[k].equals(animals[j])){
}
}
uniqueArray[i]=animals[i];
i++;
}
}
return uniqueArray;
}
Explanation / Answer
private static int removeDuplicates(int[] animals)
{
if(animals.length<2)
return animals.length;
int j = 0;
int i=1;
while(i<animals.length){
if(animals[i]== animals[j])
{
i++;
}else
{
j++;
animals[j]= animals[i];
i++;
}
}
return j+1;
}