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!

Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.

Do not allocate extra space for another array, you must do this in place with constant memory.

For example,
Given input array A = [1,1,2],

Your function should return length = 2, and A is now [1,2].

asked 16 Feb '12, 12:47

1337c0d3r's gravatar image

1337c0d3r ♦♦
1.2k1384171
accept rate: 2%


12next »

    int removeDuplicates(int A[], int n) {
    // Start typing your C/C++ solution below
    // DO NOT write int main() function
    int i=0;
    int j;
    if (n<=1) return n;

    for (j=1;j<n;j++)
    {            
        if (A[j] != A[i])
        {                
            A[++i]=A[j];
        }
    }
    return i+1;
}
link

answered 12 Jan '13, 12:32

roger's gravatar image

roger
664
accept rate: 0%

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

answered 15 Jan '13, 02:37

BlackMamba's gravatar image

BlackMamba
11124
accept rate: 0%

if( n <= 1)
  return n;
int begin = 0;
int end = 1;
while( end < n )
{
    if( A[begin] == A[end])
        end++;
    else 
        {
            A[++begin] = A[end++];
        }
}
return begin+1;
link

answered 15 Jan '13, 06:50

spooky's gravatar image

spooky
124
accept rate: 0%

public int removeDuplicates(int[] A) {

    if(A == null || A.length == 0)
        return 0;

    int element = A[0];
    int index = 0;

    for(int i = 0; i < A.length; i++) {
        if(element != A[i]) {
            index++;
            A[index] = A[i];
            element = A[i];
        }
    }

    return index + 1;
}
link

answered 24 Jan '13, 23:42

entourage's gravatar image

entourage
5114
accept rate: 0%

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

answered 20 Feb '13, 12:48

27606's gravatar image

27606
83
accept rate: 0%

class Solution {
public:
    int removeDuplicates(int A[], int n) {
        int j = 0;
        for (int i = 1; i < n; ++i) {
            if (A[i] != A[j]) {
                A[++j] = A[i];
            }
        }
        // n doesn't like to be checked at the very top :(
        return (++j) * (!!n);
    }
};
link

answered 11 Mar '13, 03:48

Ark's gravatar image

Ark
57367
accept rate: 11%

edited 11 Mar '13, 03:51

int removeDuplicates(int A[], int n) {
    //ptr point to the last element of non-duplicate array
    if(n==0) return 0;
    if(n==1) return 1;
    int ptr = 0;
    int i;
    for(i=1;i<n;i++){
        if(A[i]==A[ptr]){
        }else{
            A[++ptr] = A[i];
        }
    }
    return ptr+1;
}
link

answered 11 Mar '13, 05:20

morganyuan's gravatar image

morganyuan
111
accept rate: 0%

int removeDuplicates(int A[], int n) {
    int cur = 0;
    if(n==0) {
        return n;
    }
    for(int i=1; i<n; i++) {
        if(A[i] != A[cur]) {
            A[++cur] = A[i];
        }
    }
    return cur+1;
}
link

answered 02 Apr '13, 14:04

cookid's gravatar image

cookid
314
accept rate: 0%

Use two pointers, one head, one next. If A[head] == A[next], then move next forward. Otherwise, copy A[next] to A[head+1], then increment both head and next.

int removeDuplicates(int A[], int n) {
    // Start typing your C/C++ solution below
    // DO NOT write int main() function
    if (n == 0 || n == 1) {
        return n;
    }

    int hd = 0, nx = 0;

    for (; nx != n;) {
        if (A[hd] == A[nx]) {
            nx++;
        } else {
            A[++hd] = A[nx++];
        }
    }

    return hd+1;
}
link

answered 04 Apr '13, 00:21

whitebluff's gravatar image

whitebluff
212
accept rate: 0%

int removeDuplicates(int A[], int n) {
    if (n == 0) return 0;
    int j = 0;
    for (int i = 1; i < n; ++i)
        if (A[i] != A[j])
            A[++j] = A[i];
    return j + 1;
}
link

answered 28 May '13, 12:06

Lu-An%20Gong's gravatar image

Lu-An Gong
44846
accept rate: 7%

edited 28 May '13, 12:20

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:

×133

Asked: 16 Feb '12, 12:47

Seen: 5,751 times

Last updated: 22 Oct '13, 08:42