remove element (removes duplicate elements, loops over 2 and more than 2 elements)

Source: Internet
Author: User

Given an array and a value, remove all instances of that value in place and return the new length.

The order of elements can be changed. It doesn ' t matter what are you leave beyond the new length.

Time complexity O (n), Spatial complexity O (1). The idea of solving problems is mainly the idea of double pointers, one for cursor traversal and one for the last element of the new array:

public int removeelement (int[] A, int elem) {        int j =-1;        for (int i = 0; i < a.length; i++) {        if (a[i]! = elem) {        a[++j] = A[i];        }        }        return j+1;    }

This method of thinking is very important, because many of the problems are the deformation of the problem, particularly troublesome.

Deformation problem, if the deletion is not an element, but two? For example, delete 1, 2. Examples of circular deletions must be considered, such as 21112223, after removing 1, 2 elements, it becomes 23, because after the middle 23 is deleted, the array has successive elements.

The difficulty inside on the problem went up, but with the last problem of the bedding, the following is better solution. But there is a difference, the main idea is the structure of the stack. First on the code:

public int removeelement (int[] A, int elem1, int elem2) {    int i =-1;    for (int j = 0; J < A.length; J + +) {    if (a[j]! = elem2) {    a[++i] = a[j];    } else if (i >= 0 && a[ I] = = elem1) {//a[i] = = Elem1 && A[j] = = elem2    i--;    } else {//a[i]! = elem1 && A[j] = =    elem2 A[++i] = A[j];    }    }    return i+1;    }

Which used the idea of the stack. But instead of opening up the stack space, imitate the stack on the basis of the original array. I always point to the top of the stack, and J is the element that will be stacked. There are three main judgments:

The first if statement means that if you do not encounter elem2, the element continues to stack.

The meaning of the first if statement is that if at this point the top element of the stack is elem1, and the element that is going into the stack is elem2, a continuous element elem1,elem2 is formed. So the stack, I--。

The second if statement means: If the top of the stack is not elem1, continue into the stack. Of course, it is more concise to merge the first one with the second if statement, but the code is more difficult to read.



remove element (removes duplicate elements, loops over 2 and more than 2 elements)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.