Old discuss is read-only now. Please go to New LeetCode Discuss for your questions and answers!

User account in old discuss will not integrate to the new one, but new discuss is integrated with new online judge, which means, if you already have an account in new online judge, you can access new discuss immediately!

If you want to ask for question relevant to the posts in old discuss, please copy content as part of your question, only old discuss link is NOT ALLOWED!

Please read the FAQ in new LeetCode Discuss to help yourself making the best use of Discuss!

0
1

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 you leave beyond the new length.

asked 16 Feb '12, 23:18

1337c0d3r's gravatar image

1337c0d3r ♦♦
1.2k1084171
accept rate: 2%


12next »

class Solution {
public:
int removeElement(int A[], int n, int elem) {
    // Start typing your C/C++ solution below
    // DO NOT write int main() function
int len = 0;
for (int i = 0; i < n; i++)
{   
    if (A[i] != elem)
        A[len++] = A[i];
}
return len;
}
};
link

answered 15 Jan, 02:18

BlackMamba's gravatar image

BlackMamba
11124
accept rate: 0%

Exactly the same as you, not a single word is different.

(16 Jan, 22:55) spooky spooky's gravatar image
public class Solution {
 public int removeElement(int[] A, int elem) {
    int len=A.length;
    for(int i=0;i<len;){
        if(A[i]==elem)
            A[i]=A[--len];
        else
            i++;            
    }
    return len;
 }

}

link

answered 31 Dec '12, 16:26

sunfaquir's gravatar image

sunfaquir
613
accept rate: 0%

    int removeElement(int A[], int n, int elem) {
    // Start typing your C/C++ solution below
    // DO NOT write int main() function
    int i=-1,j;
    for (j=0;j<n;j++)
    {
        if (A[j] != elem)
        {
            A[++i]=A[j];                
        }
    }
    return i+1;
}
link

answered 12 Jan, 15:12

roger's gravatar image

roger
664
accept rate: 0%

class Solution {
public:
   int removeElement(int A[], int n, int elem) {
    // Start typing your C/C++ solution below
    // DO NOT write int main() function
    int len = 0;
    for(int i=0;i<n;i++)
    {
        if( A[i] != elem)
            A[len++] = A[i];
    }
    return len;
 }

};

link

answered 16 Jan, 22:54

spooky's gravatar image

spooky
124
accept rate: 0%

keep the order of the array

int removeElement(int A[], int n, int elem) {
    // Start typing your C/C++ solution below
    // DO NOT write int main() function
    int retVal =0;
    int ix = 0; 
    for(int i=0; i<n; i++){
        if(A[ix] == elem){
            for(int j = ix; j<n-1; j++){
                *(A+j) = *(A+j+1);
            }
            ix--;
        }
        else{
            retVal++; 
        }
        ix++;
    }
    return retVal; 
}
link

answered 21 Feb, 10:00

27606's gravatar image

27606
83
accept rate: 0%

edited 21 Feb, 10:04

public class Solution {

public int removeElement(int[] A, int elem) {
    if(A == null || A.length == 0)
        return 0;
    int size = A.length;
    for(int i = A.length-1;i>=0;i--){
        if(A[i] == elem){
            A[i] = A[--size];
        }
    }
    return size;
}

}

link

answered 15 Apr, 15:30

Xin%20%20Zeng's gravatar image

Xin Zeng
11
accept rate: 0%

edited 15 Apr, 15:31

int removeElement(int A[], int n, int elem) {
    // Start typing your C/C++ solution below
    // DO NOT write int main() function
    int head = 0, tail = n-1;
    for (int i = n-1; i >= 0; i--) {
        if (A[i] == elem) {
            continue;
        }
        A[tail--] = A[i];
    }

    for (int i = tail+1; i < n; i++) {
        A[head++] = A[i];
    }

    return head;
}
link

answered 28 Apr, 22:09

whitebluff's gravatar image

whitebluff
212
accept rate: 0%

class Solution {

public:

int removeElement(int A[], int n, int elem) {
    // Start typing your C/C++ solution below
    // DO NOT write int main() function
    int i; 
    int k1, k2; 
    k1=0; k2=0; 
    while(k2<=n-1)
    {
     if(A[k2]==elem) k2++; 
     else 
      { A[k1]=A[k2]; k1++; k2++; }    
    }
    return k1; 
}

};

link

answered 01 May, 11:35

cpp's gravatar image

cpp
103
accept rate: 0%

int removeElement(int A[], int n, int elem) {
    if (n <= 0) return 0;
    int num = 0;
    while (A[num] != elem && num < n) num++;
    if (num == n) return n;
    for (int i = num + 1; i < n; ++i) {
        if (A[i] != elem) {
            A[num++] = A[i];
        }
    }
    return num;    
}
link

answered 14 Jul, 02:29

sploving's gravatar image

sploving
113
accept rate: 0%

Since there's no need to actually 'free' the unused memory, you just have to pull all the valid values form the end of the array to the places where the target element is; then, re-check that slot in case in was a target element too.

class Solution {
public:
    int removeElement(int A[], int n, int elem) {
        for(int i = 0; i < n; i++){
            if(A[i] == elem)
                A[i--] = A[--n];
        }
        return n;
    }
};
link

answered 12 Aug, 13:30

lal0l's gravatar image

lal0l
214
accept rate: 0%

edited 12 Aug, 13:33

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • Indent code by 4 spaces.
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×137

Asked: 16 Feb '12, 23:18

Seen: 1,946 times

Last updated: 26 Sep, 03:54