Duplicates in a sorted java array

前端 未结 5 1981
傲寒
傲寒 2021-01-07 12:31

I have to write a method that takes an array of ints that is already sorted in numerical order then remove all the duplicate numbers and return an array of just the numbers

5条回答
  •  轮回少年
    2021-01-07 12:42

    public int[] noDups(int[] arr){
    
        int j = 0;
        // copy the items without the dups to res
        int[] res = new int[arr.length];
        for(int i=0; i

    First loop is O(n) and so is the second loop - which totals in O(n) as requested.

提交回复
热议问题