Remove duplicates from integer array

前端 未结 23 2406
执念已碎
执念已碎 2020-12-01 18:52

I having a problem with coding this:

Write a static method named removeDuplicates that takes as input an array of integers and returns as a result a new

23条回答
  •  孤城傲影
    2020-12-01 19:22

    Try this

    public static int[] removeDuplicates(int[] s) {     
        Integer[] array = new HashSet(Arrays.asList(ArrayUtils.toObject(s))).toArray(new Integer[0]);      
        return ArrayUtils.toPrimitive(array);
    }
    

    Edit: Updated with Apache Lang to convert to primitives.

提交回复
热议问题