Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to delete duplicates in a sorted array in LeetCode

Shulou Source: shulou.com Published: 2022-06-02 05:51:04 10月04日 Update

This article introduces how to delete duplicates in the sorted array in LeetCode. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

First, delete the duplicates in the sorted array

1. Brief introduction of the problem.

Given a sorted array, you need to delete the repeating elements in place so that each element appears only once, returning the new length of the removed array.

Do not use extra array space, you must modify the input array in place and do it using O (1) extra space.

2, example description example 1:

Given array nums = [1, 1, 1, 2]

The function should return the new length 2, and the first two elements of the original array nums are modified to 1, 2.

You don't need to consider the elements in the array that exceed the new length.

Example 2:

Given nums = [0re0jing1jing1jing1jing2jing2jinme3jpj4]

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.

You don't need to consider the elements in the array that exceed the new length.

Description:

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

Note that the input array is passed as a 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 as a "reference". That is, no copy of the argument 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 out all the elements in the array within that length range.

For (int I = 0; I < len; iTunes +) {

Print (nums [I])

}

3, the way to solve the problem

Two ways of thinking: (1) for the first time, when traversing the array, let the first element act as a "sentinel" node, and then traverse the data to compare the following elements, but remember to update the data of the "sentry" node every time you traverse the array; (2) operate on the collection according to the hashMap key

4, problem solving procedure

Import java.util.HashMap

Import java.util.Map

Public class RemoveDuplicatesTest2 {

Public static void main (String [] args) {

Int [] nums = {1,1,2}

Int removeDuplicates = removeDuplicates2 (nums)

System.out.println ("removeDuplicates =" + removeDuplicates)

}

Public static int removeDuplicates (int [] nums) {

Int len = nums.length

If (0 = = len) {

Return 0

}

Int prev = nums [0]

Int newLen = 1

For (int I = 1; I < len; iTunes +) {

Int num = nums [I]

If (num! = prev) {

Nums [newLen++] = num

Prev = num

}

}

Return newLen

}

Public static int removeDuplicates2 (int [] nums) {

If (nums = = null | | nums.length = = 0) {

Return 0

}

Map map = new HashMap (nums.length)

Int index = 0

For (int I = 0; I < nums.length; iTunes +) {

Int value = map.getOrDefault (nums [I], 0) + 1

Map.put (nums [I], value)

If (value

Tags: Array element length function sort input example content in place Sentinel idea data way more user Space Node Super help incorrect Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Huawei vpn MariaDB Shulou Information