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;  
    }
}




69 Comments


Ella Baker
Ella Baker
5 days ago

Wow!! What a collection you have. It's surely recommendable. You also may know about the Brussels Airlines Kampala Office provides reliable customer support and travel assistance for passengers flying with the airline. Travelers can contact the office for help with flight bookings, ticket changes, baggage inquiries, refund requests, and check-in services. The office also offers updated information regarding flight schedules, travel policies, and special assistance services. By reaching out to the Brussels Airlines Kampala Office, passengers can receive professional guidance and enjoy a smooth and comfortable travel experience.


Like

Hello! Great explanation on handling sorted arrays and duplicate removal techniques. The coding examples were easy to follow, and I also enjoyed reading this while searching for information related to Ayurvedic Pain Relief Cream. Keep sharing such informative content!

Like

Superb post. I have already said that you are a great author. Thanks for sharing this kind of content. You may discover more about DFW Airport Currency Exchange was really impressed with their expanding network and affordable routes. The airport offers currency exchange counters and ATMs located across various terminals, making it convenient to access cash or exchange foreign currency. Services are typically available both before and after security, depending on the terminal.

Like

Mình có lần lướt đọc mấy trao đổi trên mạng شيخ روحاني thì thấy nhắc nên cũng tò mò mở ra xem thử cho biết. Mình không tìm hiểu sâu rauhane chỉ xem qua trong thời gian ngắn để quan sát bố cục s3udy cách sắp xếp các mục và trình bày nội dung tổng thể. Cảm giác là các phần được trình bày khá gọn, các mục rõ ràng nên đọc lướt cũng không bị rối Berlinintim, với mình như vậy là đủ để nắm   tin cơ bản rồi. q8yat

Like

Nice article! A Riva Side Table is definitely a smart choice for anyone who needs a compact storage solution. I especially like the side table 2 drawers design because it keeps things neat and organized. Small furniture pieces like this really help maximize space in modern homes.


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