Remove Duplicates from Sorted Array in Place

Day 9 of my #50DaysOfCode challenge is done ✅ 📌 Problem Solved Remove Duplicates from a Sorted Array The array was already sorted. We had to keep only unique elements. And do it in-place. Let’s understand with real life example ◾️Imagine you are arranging certificates in a file. ◾️Some certificates are duplicated. ◾️You only keep one copy of each. ◾️You arrange them neatly at the front. ◾️Whatever remains at the back does not matter. That’s the idea. 💻Optimal Approach (Using Set) 🔹️Create a set. 🔹️Insert all elements of the array into the set. 🔹️Set automatically removes duplicates. 🔹️Size of set gives number of unique elements (k). 🔹️Traverse the set. 🔹️Fill first k indices of the array with unique elements. Clean and simple. 📊 Complexity Analysis ▫️Time Complexity: O(N) We traverse the array once. ▫️Space Complexity: O(1) Constant extra space used. 📚 What I learned today: ▫️Sorted arrays make duplicate problems easier. ▫️Set is very useful for handling uniqueness. ▫️Understanding constraints helps in choosing approach. #50DaysOfCode #CodingChallenge #Consistency #LearningInPublic

To view or add a comment, sign in

Explore content categories