Get the App
SLTechnology News&Howtos  ›  Development  › 

How does JavaScript delete duplicates in an ordered array

Shulou Source: shulou.com Published: 2022-06-01 03:03:57 09月22日 Update

In this article Xiaobian for you to introduce in detail "JavaScript how to delete duplicates in the ordered array", the content is detailed, the steps are clear, the details are handled properly, I hope this "JavaScript how to delete duplicates in the ordered array" article can help you solve your doubts, the following follow the editor's ideas slowly in-depth, together to learn new knowledge.

If you have an ordered array nums, you are required to delete the repeating elements in place so that each element appears only once, and returns the new length of the deleted array.

Do not use extra array space, it must be done by modifying the input array in place and using O (1) extra space.

Description:

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

Note: 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]);}

Example 1:

Input: nums = [1, 1, 1, 1, 2]

Output: 2, nums = [1BI 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. You do not need to consider the elements in the array that exceed the new length.

Example 2:

Input: nums = [0re0pence1jing1jing1jing2jing2jinme3re4]

Output: 5, nums = [0pm 1pm 2jue 3jue 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. You do not need to consider the elements in the array that exceed the new length.

Note:

0

Tags: Array element pointer length input order function point in place article space output location content idea way user example Chinese super explanation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Shulou Technology Microsoft Redmi vpn