Algorithm - delete duplicates in the sorting array [2021-10-16]

Keywords: Algorithm

2. Delete duplicates in the sort array

2.1 Title Description

Given a sort array, you need to   In situ Delete the repeated elements so that each element appears only once, and return the new length of the array after removal.

Do not use additional array space, you must In situ   Modify the input array and do it with O(1) extra space.

Example 1:

  Given array num = [1,1,2], 
 ​
  The function should return a new length of 2, and the first two elements of the original array num are modified to 1 and 2. 
 ​
  You don't need to consider the elements in the array that exceed the new length.

Example 2:

  Given num = [0,0,1,1,1,2,2,3,3,4],
 ​
  The function should return a new length of 5, and the first five elements of the original array num are modified to 0, 1, 2, 3 and 4.
 ​
  You don't need to consider the elements in the array that exceed the new length.

explain:

Why is the returned value an integer, but the output answer is an array?

Note that the input array is passed by reference, which means that modifying the input array in the function is visible to the caller.

You can imagine the internal operation as follows:

// nums is passed by reference. That is, no copy of the arguments is made
int len = removeDuplicates(nums);

// Modifying the input array in the function is visible to the caller.
// According to the length returned by your function, it will print all elements within that length range in the array.
for (int i = 0; i < len; i++) {
    print(nums[i]);
}

2.2 solution (double pointer solution):

Idea:

After the array is sorted, we can place two pointers I and j, where I is the slow pointer and j is the fast pointer. As long as num [i] = num [J], we increase J to skip duplicates.

When we meet nums[j]= When num [i], the run of skipping duplicates has ended, so we must copy the value of it (Num [j]) to num [i + 1]. Then increment I, and then we'll repeat the same process again until j reaches the end of the array.

Official solution:

 public int removeDuplicates(int[] nums) {
     if (nums.length == 0) {
         return 0;
     }   
     int i = 0;
     for (int j = 1; j < nums.length; j++) {
         if (nums[j] != nums[i]) {
             i++;
             nums[i] = nums[j];
         }
     }
     return i + 1;
 }

 ​

Complexity analysis

  • Time complexity: O(n). Assuming that the length of the array is n, i and j traverse at most N steps respectively.

  • Space complexity: O(1).

Write your own method:

 public class DeleteArrayDuplicates {
     public static void main ( String[] args ) {
 ​
         int[] arr = {1,1,2,2,2,3,3,4,8,8,8,8,8,9,9,9,10};
         int[] ints = delete ( arr );
         for ( int i : ints ) {
             System.out.print( i + " " );
         }
 ​
     }
 ​
     public static int[] delete(int[] arr){
         HashMap<Integer, Integer> hashMap = new HashMap<> ( );
         //1. Save the array in the hashMap and use the value of the array as the key
         for ( int i = 0 ; i < arr.length ; i++ ) {
             hashMap.put ( arr[i],i);
         }
 ​
         //2. Take out the key and convert it into an array
         Set<Integer> set = hashMap.keySet ( );
         Object[] array = set.toArray ();
         
         //3. Create a new array
         int[] arrs = new int[array.length];
         //4. Save the extracted key into a new array
         for ( int i = 0 ; i < array.length ; i++ ) {
             arrs[i] = (Integer) array[i];
         }
         //5. Assign the new array to the old array
         arr = arrs;
         System.out.println ("The length of the array is:" + arrs.length );
         return arr;
     }
     
 }

Complexity analysis

  • Time complexity O(n)

  • Spatial complexity O(n)

Posted by [e]r!k on Sat, 16 Oct 2021 10:40:21 -0700