Community

Start a Thread


Notifications
Subscribe You’re not receiving notifications from this thread.

Remove Duplicates from Sorted Array (Main Thread)

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

  1. First Scenario:
    • Original Array: num = [1, 2, 3, 3, 4, 4, 5, 6, 6, 6, 8, 8]
    • Expected Outcome:
      • Length of Resultant Array: 8
      • Resultant Array: [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)

  1. Second Scenario:
    • Original Array: num = [1, 2, 2, 3, 4, 4, 4, 5, 5]
    • Expected Outcome:
      • Length of Resultant Array: 5
      • Resultant Array: [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

  • The input array, num, is sorted in a non-decreasing manner.
  • The length of 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

Jake from AlgoDaily Commented on Sep 10, 2022:

This is the main discussion thread generated for Remove Duplicates from Sorted Array (Main Thread).