#26 Remove duplicates from Sorted Array

Source: Internet
Author: User

Title Link: https://leetcode.com/problems/remove-duplicates-from-sorted-array/


Given a sorted array, remove the duplicates in place such, all element appear only once and return the new L Ength.

Do the allocate extra space for another array, and you must does this on place with constant memory.

for example,
given input Array nums  = [1,1,2] ,

Your function should return length = 2 , with the first of the elements of nums being and 1 2 Respectivel Y. It doesn ' t matter what are you leave beyond the new length.


int RemoveDuplicates (int* nums, int numssize) {    if (numssize <= 1)                   //if less than one element, no repeating meta        return numssize;    int index = 0;                      Mark the last element after deleting the duplicate for the subscript for    (int i = 1; i < numssize; ++i)   //traversal array, if the current element is not the same as the previous one, join to the processed array if        (nums[i]! = nums[ Index])            nums[++index] = nums[i];    The deleted array is always smaller than the original array, so you can store the return index + 1 directly on the original array    ;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

#26 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.