Remove duplicates from sorted Array

Source: Internet
Author: User
Remove duplicates from sorted array total accepted: 22879 total submissions: 701_my submissions

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 shocould return length =2, And a is now[1,2].

This is similar to removing duplicates from sorted list, but it is replaced with an array. You need to change the elements of the sorted array to non-repeating and require in-situ operations.

class Solution {public:    int removeDuplicates(int A[], int n) {        int new_arr_cur, arr_cur;        new_arr_cur = arr_cur = 0;                while (arr_cur < n) {            A[new_arr_cur++] = A[arr_cur]; // assign            //skip duplicates            while (arr_cur < n && A[arr_cur] == A[new_arr_cur - 1]) {                arr_cur++;            }        }                return new_arr_cur;    }};

Remove duplicates from sorted array II Total accepted: 17014 total submissions: 55366my submissions

Follow up for "remove duplicates ":
What if duplicates are allowed at most twice?

For example,
Given sorted array A =[1,1,1,2,2,3],

Your function shocould return length =5, And a is now[1,1,2,2,3].

The preceding code can be modified at most twice.

class Solution {public:    int removeDuplicates(int A[], int n) {        int new_arr_cur, arr_cur, count;        new_arr_cur = arr_cur = 0;        const int DUPLICATE_NUM = 2;        while (arr_cur < n) {            A[new_arr_cur++] = A[arr_cur++];            count = 1;            //add allowed duplicates            while (count < DUPLICATE_NUM && arr_cur < n && A[arr_cur] == A[new_arr_cur - 1]) {                A[new_arr_cur++] = A[arr_cur++];                count++;            }            //skip leftmost duplicate            while (arr_cur < n && A[arr_cur] == A[new_arr_cur - 1]) {                arr_cur++;            }        }        return new_arr_cur;    }};


Remove duplicates from sorted Array

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.