top of page
Programming

Remove duplicates from sorted array




Welcome to another episode in our "Must Do Coding Interview Questions" playlist! In this video, I have coded for "Removing duplicates from a sorted array". Solving this coding problem is a great way to sharpen your DSA problem-solving skills.


First I have explained a brute-force solution to solve this problem statement. This involves using a map. Then I have explained a more optimal solution that makes use of 2-pointers. This problem statement is present in leetcode.


I'll be coding in Java, and by the end of this video, you'll have a deep understanding of these techniques and be well-prepared for your next coding interview.


This question has 2 variations. The easy variation is covered in this video and the 2nd variation is covered in the next video


This playlist will cover all the must-do interview questions for cracking the coding interview round (including for companies like Meta,/Facebook, Apple, Amazon, Netflix, and Google)


Don't forget to like, share, and subscribe for more coding interview tutorials!

Happy Coding :)



class Solution {
    public int removeDuplicates(int[] nums) {
        int i = 0;
        for (int j = 1; j < nums.length; ) {
            if(nums[i] == nums[j]){
                j++;
            }
            else{
                i++;
                nums[i] = nums[j];
            }
        }
        return i + 1;
    }
}




class Solution {
    public int removeDuplicates(int[] nums) {
        int n=nums.length;
        if(n<=2) return n;

        int i=2;
        for(int j=2;j<n;j++)
        {
            if(nums[j]!=nums[i-2])
            {
                nums[i]=nums[j];
                i++;
            }
        } 
        return i;  
    }
}




60 Comments


Such a beautifully written article! I love how you emphasized the importance of choosing wall art that connects emotionally. Your point about the psychological effect of colors really opened my eyes. I’ve recently started exploring more abstract wall art, and I can already see how it changes the mood of my living space. The practical design suggestions you shared — especially on spacing and size — are things most people overlook. Thanks for providing such an informative and visually inspiring read. Truly one of the best décor blogs I’ve come across!

Like

Some relationships carry emotions beyond expression. bhai bahan antarvasna uncovers unspoken feelings and human complexities that challenge norms. These stories explore forbidden emotions and the delicate balance between affection, curiosity, and desire in their rawest form.

Like

Bring excitement into your bedroom with sex toy for men specially designed for ultimate stimulation. Enjoy realistic sensations, adjustable vibrations, and ergonomic designs that target sensitive zones perfectly. Perfect for solo play or couple adventures, these toys elevate every intimate experience effortlessly.

Like

Such a well-written post! The Nesting-Table is my go-to solution for small spaces. It looks elegant and is super easy to move around. I’m happy to see more people recognizing its benefits. Keep sharing these helpful décor tips!

Like

"Very helpful article! Your explanation about comparing offers and checking after-sales support is exactly what I needed. Now I feel confident exploring the best Diwali furniture sale offer 2025 for my home."

Like
download (7)_edited.png
Subscribe to my Youtube Channel @codewithgd

Related Articles

Videos you might like

Let's Get
Social

  • alt.text.label.Twitter
  • alt.text.label.LinkedIn
  • 25231
Subscribe to our NewsLetter

Join our mailing list to get a notification whenever a new blog is published. Don't worry we will not spam you.

bottom of page