Question: (C PROGRAMMING) Given a sorted array, remove the duplicates in-place such that each element appear only once and return the new length. Do not allocate

(C PROGRAMMING)

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 by modifying the input array in-place

with O(1) extra memory.

Now, What if duplicates are allowed at most twice?

For example,

Given sorted array nums = [1,1,1,2,2,3],

Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It

doesn't matter what you leave beyond the new length.

Test Cases:

Input : [3, 1, 1, 4, 3, 1, 2, 4]

Output: [3, 1, 1, 4, 3, 2, 4]

Input : [5, 5, 5, 5, 5, 5]

Output: [5, 5]

Input : [2, 2, 1, 3, 1, 3]

Output: [2, 2, 1, 3, 1, 3]

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!