去重

2022-03-29  本文已影响0人  好名字_4d27
  public static void main(String[] args) {
        int [] arr = {6,2,13,4,5,2,2,4,4,4};
        int count=arr.length;

        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length; j++) {
                if (arr[i]<arr[j]){
                    arr[i]=arr[i]^arr[j];
                    arr[j]=arr[i]^arr[j];
                    arr[i]=arr[i]^arr[j];
                }
            }
        }

        //2 2 2 4 4 4 4 5 6 13

        //慢指针
        count = 0;
        //快指针
        for (int i = 1; i < arr.length; i++) {
            if (arr[i]!=arr[count]){
                count ++ ;
                arr[count] = arr[i];

            }
        }

        count++;

        for (int x = 0; x < count; x++) {
            System.out.printf(arr[x]+" ");
        }


    }
上一篇下一篇

猜你喜欢

热点阅读