IN MATLAB Given a sorted vector/array, write a function _removeDuplicates_, such
ID: 3825675 • Letter: I
Question
IN MATLAB
Given a sorted vector/array, write a function _removeDuplicates_, such that each element in the array appears only _once_ and return the new length.
You cannot allocate extra space for another vector/array. The removal of elements should be done in place with constant memory.
For example,
Given an input array _vec_ = [4 5 5], your function should return *2*.
YOU are not allowed to use the matlab built-in function: *unique*
>> vec = [4 5 5];
>> remove_duplicates(vec)
ans =
2
Explanation / Answer
function k = removeDuplicates(x)
l = size(x,2);
i=2;
while(i<=l)
if(x(i)==x(i-1))
x(i) = [];
l = l-1;
else
i=i+1;
end
end
k = size(x,2);
end
removeDuplicates([4,5,5])