Remove duplicates from array without using java utils.



public class RemoveDuplicates {

public static void main(String[] args) {
Integer arr[] = {1,4,2,1,7,5,0,7,7,1,1,5,4,9,4,1,5,4,9,7,6,1,7,7,8,9,9,0,4};

for(int i=0;i<arr.length-1;i++){
for(int j=i+1;j<arr.length-1;j++){
if(arr[i] == arr[j]){
for(int k = j; k<arr.length-1;k++){
arr[k] = arr[k+1];
}
                                        //copy array to remove last element.
Integer copy[] = new Integer[arr.length-1];
System.arraycopy(arr, 0, copy, 0, arr.length-1);
arr = copy;
                                       //decrease j value in case match found to not miss repeated values.
j--;
}
}
}

for(int z=0; z<arr.length;z++)
System.out.print(arr[z]);

}

}

Comments

Popular posts from this blog

BEA-000362 Server failed. Reason: [Management:141268]Parsing Failure in config.xml

Oracle OIM - Search and Update Organization using java API

Trim Leading and Trailing spaces from InputText on tab out.