Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to delete duplicates in a sorted array in java

Shulou Source: shulou.com Published: 2022-06-01 01:38:38 09月19日 Update

This article mainly shows you "how to delete duplicates in a sorted array in java", which is easy to understand and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how to delete duplicates in a sorted array in java".

Given a sorted array, you need to delete the repeating elements in place so that each element appears at most twice, 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.

Example 1:

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

The function should return the new length length = 5, and the first five elements of the original array are modified to 1, 1, 2, 2, 3.

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

Example 2:

Given nums = [0jin0jing1jing1jing1jing1jinjinjingjinshengyue 3]

The function should return the new length length = 7, and the first five elements of the original array are modified to 0,0,1,1,2,3,3.

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

Answer:

1public int removeDuplicates (int [] nums) {

2 int n = nums.length

3 if (n

< 2) 4 return n; 5 int pos = 2; 6 for (int i = 2; i < n; ++i) 7 if (nums[i] != nums[pos - 2]) 8 nums[pos++] = nums[i]; 9 return pos; 10} 解析: 题目中说的是排序的数组,那这就简单了。代码比较简单就不再介绍,下面再来看一下其他的解法。 1public int removeDuplicates(int[] nums) { 2 int i = 0; 3 for (int n : nums) 4 if (i < 2 || n >

Nums [I-2])

5 nums [iTunes +] = n

6 return i

7}

eight

9public int removeDuplicates (int [] nums) {

10 int n = nums.length, count = 0

11 for (int I = 2; I < n; iTunes +)

12 if (nums [I] = = nums [I-2-count])

13 count++

14 else

15 nums [I-count] = nums [I]

16 return n-count

17}

The solutions are more or less the same, and the basic ideas remain the same.

What are the characteristics of Java? what are the characteristics of Java? what is the 1.Java language that represents the static object-oriented programming language? it implements the object-oriented theory and allows programmers to carry out complex programming in an elegant way of thinking. 2.Java has the characteristics of simplicity, object-oriented, distributed, security, platform independence and portability, dynamic and so on. 3. Using Java, you can write desktop applications, Web applications, distributed systems, embedded system applications, and so on.

The above is all the content of the article "how to remove duplicates in a sorted array in java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Array element sort length program content object application feature article application function distribution in situ example space system solution language Chinese super Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Shulou Technology Xiaomi NVidia Apple