Here is the interview question prompt, presented for reference.
The Challenge: Unique Array Elements
You are given a sorted array of integers, named num
. This array is special because it's sorted in non-decreasing order, meaning the numbers either stay the same or increase as you move through the array. However, there's a catch: the array might have duplicates, and your job is to make each number unique.
Example Scenarios
num = [1, 2, 3, 3, 4, 4, 5, 6, 6, 6, 8, 8]
[1, 2, 3, 4, 5, 6, 8]
![Remove Duplicate Elements from the array](https://storage.googleapis.com/algodailyrandomassets/curriculum/remove-duplicates-from-sorted-array/image1.png)
num = [1, 2, 2, 3, 4, 4, 4, 5, 5]
[1, 2, 3, 4, 5]
![Remove Duplicate Elements from the array](https://storage.googleapis.com/algodailyrandomassets/curriculum/remove-duplicates-from-sorted-array/image2.png)
Your Objective
Develop a program that takes this sorted array and eliminates any duplicate elements. The challenge is to do this while maintaining the original order of the elements.
Key Constraints
num
, is sorted in a non-decreasing manner.num
is always greater than 0.Your task is to return two things: the length of the new array and the array itself, now with unique elements. This problem is a great way to understand array manipulation and the importance of maintaining order in data structures.
You can see the full challenge with visuals at this link.
Challenges • Asked about 2 years ago by Jake from AlgoDaily
This is the main discussion thread generated for Remove Duplicates from Sorted Array (Main Thread).