Topic:
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 A = [1,1,2]
,
your function should return length = 2
, and A is Now
Idea: Traverse from the beginning, traverse it again, and then
#include <iostream> #include <vector>using namespace std;/* removes the repeating element in a sorted array and returns the length */int removeduplicate ( vector<int>& VEC) {if (Vec.size () <=1) return vec.size (); int i=0,j;for (J=1;j<vec.size (); j + +) if (Vec[j] ! = Vec[j-1]) {i++;vec[i] = vec[j];} return i+1;} int main () {int array[]={1,1,1,1,2,2,3,4,4,5,6,7};vector<int> Vec (array,array+sizeof (array)/sizeof (int)); Cout<<removeduplicate (VEC); return 0;}
Remove Duplicates from Sorted Array--leetcode