Remove Duplicates from Sorted Array in Python

🚀 Day 2 – DSA Series Remove Duplicates from Sorted Array Solved LeetCode 26 – Remove Duplicates from Sorted Array using Python. 🧠 Problem Summary Given a sorted array nums, remove duplicates in-place such that each unique element appears only once. Return the number of unique elements k, where: • The first k elements of nums contain the unique values • The remaining elements beyond k don’t matter • No extra array allowed (O(1) space) Example: Input: [1, 1, 2] Output: k = 2 → Modified array: [1, 2, _] 💡 Approach Used (Two Pointer Technique) Since the array is sorted, duplicates are adjacent. ✔️ Initialized k = 1 (first element always unique) ✔️ Iterated from index 1 ✔️ Compared current element with previous element ✔️ When a new unique element is found: • Placed it at index k • Incremented k This ensures in-place modification without extra space. ⏱ Complexity Analysis Time Complexity: O(n) – single pass Space Complexity: O(1) – no additional data structures 🔎 Key Takeaway This problem strengthens: • Two Pointer pattern • In-place array manipulation • Understanding problem constraints carefully Continuing the DSA series — one problem at a time. 🚀 #DSA #LeetCode #Python #Algorithms #TwoPointers #ProblemSolving #CodingJourney

  • graphical user interface, text

To view or add a comment, sign in

Explore content categories