Showing posts with label How to sort and remove the duplicates from array without using sort() method.. Show all posts
Showing posts with label How to sort and remove the duplicates from array without using sort() method.. Show all posts

Tuesday, 14 April 2015

How to sort and remove the duplicates from array without using sort() method.

import org.apache.commons.lang3.ArrayUtils;

public class SortAndRemoveDuplicate {
    public static void main(String[] args) {
        int[] a = {1,5,3,2,1,11,7,5};
        int l = a.length, s=0;        
        for(int j=0; j<l; j++){
            for(int i=0; i<l-1; i++){
                if(a[i]>a[i+1]){
                    s = a[i];
                    a[i] = a[i+1];
                    a[i+1] = s;
                }else if(a[i]==a[i+1]){
                    a = ArrayUtils.remove(a, i);
                    l = a.length;
                }
            }
        }
        for(int n: a){
            System.out.println(n);
        }
    }
}