Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to delete duplicates in an ordered array

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article will explain in detail how to delete duplicates in an ordered array. The quality of the article is high, so Xiaobian shares it with you for reference. I hope you have a certain understanding of relevant knowledge after reading this article.

topic

Give you an ordered array numbers, delete duplicate elements in place, so that each element appears only once, and return the new length of the deleted array.

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

description

Why is the return 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 inner workings as follows:

// nums are passed by reference. int len = removeDuplicates(numbers);//modifying the input array in the function is visible to the caller.// Depending on the length returned by your function, it prints out all elements of the array within that length range. for (int i = 0; i < len; i++) { print(nums[i]);}

Example 1:

Input: nums = [1,1,2] Output: 2, nums = [1,2] Explanation: The function should return a new length of 2, and the first two elements of the original array nums are modified to 1, 2. There is no need to consider elements in the array beyond the new length.

Example 2:

Input: nums = [0,0,1,1,2,2,3,3,4] Output: 5, nums = [0,1,2,3,4] Explanation: The function should return a new length of 5, and the first five elements of the original array nums are modified to 0, 1, 2, 3, 4. There is no need to consider elements in the array beyond the new length.

Tip:

0

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report