Jobs
Interviews

Solve Remove Duplicates from Sorted Array using Python Language

Solve Remove Duplicates from Sorted Array using Python to enhance your skills with python coding practice , master coding concepts, and prepare for interviews with practical exercises and detailed solutions.

Remove Duplicates from Sorted Array

Difficulty : Medium

Categories :

  • Arrays

Given a sorted array arr, modify the array in-place to contain only distinct elements and return the size of the modified array. The distinct elements should appear at the beginning of the original array.

Important Notes:

  • Do not use Set or HashMap to solve the problem
  • The array is already sorted in ascending order
  • Modify the original array in-place
  • Return only the size of the modified array (number of distinct elements)

Constraints:

  • 1 ≤ arr.length ≤ 105
  • 1 ≤ arr[i] ≤ 106

Examples:

Input: arr = [2,2,2,2,2]
Output: 1
Explanation: After removing duplicates, only one 2 remains. The first position of the modified array will contain 2.
Input: arr = [1,2,2,3,4,4,4,5,5]
Output: 5
Explanation: After removing duplicates, the first 5 positions will contain [1,2,3,4,5].

Follow-up:

Can you implement the solution with O(1) extra space and O(n) time complexity using the two-pointer technique?

Coding works best on a laptop or desktop, with a keyboard and a wider editor. You can read the problem here and solve it there.

Problem Solving

Input

What You'll Find Here

Interactive Exercises Practice coding with problems designed for beginners and experts.

Step-by-Step Solutions Understand every step of the solution process.

Real-World Scenarios Apply your skills to real-world problems and boost your confidence.

Choose from the following categories