115. Remove duplicates in a sorted array removeDuplicatesFromSortedArray

Question description

Question link

Given an array nums arranged in ascending order, please delete the repeated elements in place so that each element appears only once, and return the new length of the array after deletion. The relative order of elements should remain consistent. Then returns the number of unique elements in nums.

Considering that the number of unique elements of nums is k, you need to do the following to ensure that your solution can be passed:

Change the array nums so that the first k elements of nums contain unique elements in the order in which they originally appeared in nums. The remaining elements of nums are unimportant to the size of nums.
Return k.
Judgment criteria:

The system will use the following code to test your solution:

int[] nums = [...]; // 输入数组
int[] expectedNums = [...]; // 长度正确的期望答案

int k = removeDuplicates(nums); // 调用

assert k == expectedNums.length;
for (int i = 0; i < k; i++) {
    
    
    assert nums[i] == expectedNums[i];
}

If all assertions pass, your solution will be passed.

Example 1:

Input: nums = [1,1,2]
Output: 2, nums = [1,2,_]
Explanation: The function should return the new length 2, and the first two elements of the original array nums are modified to 1, 2. Elements in the array beyond the new length do not need to be considered.

Example 2:

Input: nums = [0,0,1,1,1,2,2,3,3,4]
Output: 5, nums = [0,1,2,3,4]
Explanation: The function should return the new length 5, and the first five elements of the original array nums are modified to 0, 1, 2, 3, 4. Elements in the array beyond the new length do not need to be considered.

hint:

  • 1 <= nums.length <= 3 * 104
  • -104 <= nums[i] <= 104
  • nums is sorted in ascending order

Problem-solving ideas

Due to the disordered nature of HashSet, the ones added first may be later, so they are not considered.

  1. First create the collection and then add the first element to the collection.
  2. Traverse the nums array and directly add each element of nums to the set, due to the non-repetitive nature of the set set. Only one of the same elements will be added.
  3. Because of the ordering of the array itself and the ordering of the collection, the contents of the collection are what we want. Traverse the collection and modify the elements of the array.
  4. Just return the collection size directly.

Detailed code explanation

package question;

import java.util.LinkedHashSet;
import java.util.Set;

/**
 * @author keke
 * @version 1.0
 * @className Question115
 * @description
 * @time 2023/5/28 21:38
 */
public class Question115 {
    
    

    public static void main(String[] args) {
    
    
        // 输入数组
        int[] nums = {
    
    0, 0, 1, 1, 1, 2, 2, 3, 3, 4};
        // 长度正确的期望答案
        int[] expectedNums = new int[]{
    
    0, 1, 2, 3, 4};

        // 调用
        int k = removeDuplicates(nums);

        assert k == expectedNums.length;
        for (int i = 0; i < k; i++) {
    
    
            assert nums[i] == expectedNums[i];
        }
        System.out.println("答案正确");
    }

    private static int removeDuplicates(int[] nums) {
    
    
        Set<Integer> set = new LinkedHashSet<>();
        set.add(nums[0]);
        for (int i = 1; i < nums.length; i++) {
    
    
            set.add(nums[i]);
        }
        int i = 0;
        for (Integer integer : set) {
    
    
            nums[i++] = integer;
        }
        return set.size();
    }
}

Run screenshot

Insert image description here

Guess you like

Origin blog.csdn.net/weixin_43344151/article/details/130917954