Duplicates in a sorted java array

旧街凉风 提交于 2019-12-01 02:15:19

Since this seems to be homework I don't want to give you the exact code, but here's what to do:

  • Do a first run through of the array to see how many duplicates there are
  • Create a new array of size (oldSize - duplicates)
  • Do another run through of the array to put the unique values in the new array

Since the array is sorted, you can just check if array[n] == array[n+1]. If not, then it isn't a duplicate. Be careful about your array bounds when checking n+1.

edit: because this involves two run throughs it will run in O(2n) -> O(n) time.

Tested and works (assuming the array is ordered already)

public static int[] noDups(int[] myArray) { 

    int dups = 0; // represents number of duplicate numbers

    for (int i = 1; i < myArray.length; i++) 
    {
        // if number in array after current number in array is the same
        if (myArray[i] == myArray[i - 1])
            dups++; // add one to number of duplicates
    }

    // create return array (with no duplicates) 
    // and subtract the number of duplicates from the original size (no NPEs)
    int[] returnArray = new int[myArray.length - dups];

    returnArray[0] = myArray[0]; // set the first positions equal to each other
                                 // because it's not iterated over in the loop

    int count = 1; // element count for the return array

    for (int i = 1; i < myArray.length; i++)
    {
        // if current number in original array is not the same as the one before
        if (myArray[i] != myArray[i-1]) 
        {
           returnArray[count] = myArray[i]; // add the number to the return array
           count++; // continue to next element in the return array
        }
    }

    return returnArray; // return the ordered, unique array
}

My previous answer to this problem with used an Integer List.

Not creating a new array will surely result in nulls all over the initial array. Therefore create a new array for storing the unique values from the initial array.

How do you check for unique values? Here's the pseudo code

uniq = null
loop(1..arraysize)      
  if (array[current] == uniq)  skip
  else  store array[current] in next free index of new array; uniq = array[current]
end loop

Also as others mentioned get the array size by initial scan of array

uniq = null
count = 0
loop(1..arraysize)      
  if (array[current] == uniq)  skip
  else  uniq = array[current] and count++
end loop
create new array of size count
public static int[] findDups(int[] myArray) {
    int numOfDups = 0;
    for (int i = 0; i < myArray.length-1; i++) {
        if (myArray[i] == myArray[i+1]) {
            numOfDups++;
        }
    }
    int[] noDupArray = new int[myArray.length-numOfDups];
    int last = 0;
    int x = 0;
    for (int i = 0; i < myArray.length; i++) {
        if(last!=myArray[i]) {
            last = myArray[i];
            noDupArray[x++] = last;
        }
    }
    return noDupArray;
}
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<arr.length-2; i++){
        if(arr[i] != arr[i+1]){
            res[j] = arr[i];
            j++;
        }
    }
    // copy the last element
    res[j]=arr[arr.length-1];
    j++;
    // now move the result into a compact array (exact size)
    int[] ans = new int[j];
    for(int i=0; i<j; i++){
        ans[i] = res[i];
    }
    return ans;
}

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!