int[]数组和Sting[]数组去重的几种方式

(一)int[ ]类型的:

/*int[]数组去重复
* */
public class TIntDeleteRepeat {
    /*
      1.0 测试数组int[] 去重复
       ①:无序可以先排序 int[]  intArry ={5,4,8,2,1,10 }--->调用Arrays.sort(intArry)方法
       ②:再去重
    * */
//    public static   int[] a={1,2,4,3,9,5,6,2};

    public static void main(String[] args) {

        int[] a = {1, 2, 4, 6, 2,7,5,5,8};
        String[] b={"a","ad","b","c"};
//        Arrays.sort(a);
        int[] now=removeRepeat3(a);
        System.out.println(Arrays.toString(now));
//        TIntDeleteRepeat.removeRepeat2(afdd);

//        System.out.println(c.getClass().getName());
//        System.out.println("after "+after.length);
//        System.out.println();
//
//        System.out.println(Arrays.toString(after));
//        System.out.println(a.getClass().getSuperclass().getName());
    }
    /*
    * (1)int[]数组去重方法一 for循环嵌套:
    * */
    public static int[] removeRepeat1(int[] a){
        int[] tempArr=new int[a.length];
        int t=0;
        for (int i=0;ilength;++i){
            boolean isTrue=true;
            for (int j=i+1;jlength-1;++j) {
                if (a[i] == a[j]) {
                    isTrue = false;
                    break;
                }
            }
            if (isTrue){
                tempArr[t]=a[i];
                ++t;
            }
        }
        System.out.println(Arrays.toString(tempArr));
        int[] afterArr=new  int[t];
        System.arraycopy(tempArr,0,afterArr,0,t);
        System.out.println(Arrays.toString(afterArr));
        return afterArr;
    }

    /*
    * (2)数组去重 方法二 list--ArrayList集合,contains方法,再放回数组:
    * */

    public static int[] removeRepeat2(int[] arr){
        List list=new ArrayList<>();
        for (int i=0;ilength-1;++i){
            if (!list.contains(arr[i])){
                list.add(arr[i]);
            }
        }
        Integer[] in=(Integer[]) list.toArray(new Integer[list.size()]);
        int[] need=new int[in.length];
        int i=0;
        for (Integer integer:in){
            need[i]=integer;
            ++i;
        }
        return need;
    }


    /*
    * (三)方法3[不用自动排序]:利用Set唯一性去重,要排序的化,利用TreeSet;
    * */
    public static int[] removeRepeat3(int[] arr){
        Set set=new TreeSet<>();
        for (int i=0;ilength;++i){
            set.add(arr[i]);
        }
        int[] needArr=new int[set.size()];
        int i=0;
        for (Integer integer:set){
            needArr[i]=integer;
            ++i;
        }
        return needArr;
    }

}

(二)String[ ]数组类型的去重:

/*
* String[]数组去重复
* */
public class TStringDeleteRepeat {
    public static void main(String[] args) {
        String[] array = {"a", "d", "c", "e", "a", "f"};
        String[] need=TStringDeleteRepeat.removeRepeat3(array);
        System.out.println(Arrays.toString(need));
    }

    /*1.0 用两层for循环遍历
    * */
    public static String[] removeRepeat1(String[] str) {
        String[] temp = new String[str.length];
        int k = 0;
        for (int i = 0; i < str.length; ++i) {
            boolean flag = true;
            for (int j = i + 1; j < str.length-1; ++j) {
                if (str[i]==str[j]){
                    flag=false;
                    break;
                }
            }
            if (flag){
                temp[k]=str[i];
                ++k;
            }
        }
        String[] need=new String[k];
        System.arraycopy(temp,0,need,0,k);
        return need;
    }

    /*(二)方法2:用ArrayList去重,contains()方法去重。
    * */
    public static String[] removeRepeat2(String[] arr){
        List list=new ArrayList<>(arr.length);
        for (int i=0;ilength;++i){
            if (!list.contains(arr[i])){
                list.add(arr[i]);
            }
        }

        String [] need=new String[list.size()];
        int i=0;
        for (String s:list){
            need[i]=s;
            ++i;
        }
        return need;
    }

    /*
    * (三)方法3:利用Set唯一性,去除重复
    * */
    public static String[] removeRepeat3(String[] arr){
        Set set=new TreeSet<>();
        for (int i=0;ilength;++i){
         set.add(arr[i]);
        }
        String[] need=new String[set.size()];
        int i=0;
        for (String str:set){
            need[i]=str;
            ++i;
        }
        return  need;
    }
}

你可能感兴趣的:(算法)