Remove duplicates from Sorted Array - Leetcode - 26 - Java Solution

Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once

Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same. Then return the number of unique elements in nums.

Solution : 

[class Solution {

    public int removeDuplicates(int[] nums) {

        LinkedHashMap<Integer, Integer> linkedMap = new LinkedHashMap<>();


        for (int i = 0; i < nums.length; i++){

            linkedMap.put(nums[i], i);

        }


        int size = linkedMap.size();


        int i = 0;

        for (int j : linkedMap.keySet()){

            nums[i] = j;

            i++;

        }


        return size;

    }

}]

Explanation :

In this question, all you have to do is remove the duplicates from that array. So we use hashmap for this solution with O ( N ) time complexity. 

Also, the relative order of elements should be the same. So using Linked HashMap we will be able to keep the given order same. 

Declare a linked hashmap and put all the elements into the linked hashmap where the key of the hashmap is the element in the array and the value is the index.

The duplicates have already been removed. All you have to do is put all elements in the hashmap back into the array using a for loop and return the array. 

COMMENTS

BLOGGER
Name

Affiliate marketing tutorial,2,Android,60,Android Development,22,Android tutorials,41,Blogging tactics,8,Deals,1,DSA,4,Elite Youtube Tutorial,4,Firebase,1,Flutter,4,Fuchsia,1,Hosting Guides,3,Leetcode,4,Miscellaneous,10,Mobile Gaming,17,NTN,94,Problogging,7,PUBG,16,Reviews,1,SEO Tutorial,5,Start a blog,9,Start a channel,6,Top 5,4,Travel Blogging,4,Windows,12,Windows Security,4,Windows tutorials,7,Wordpress Tutorials,4,Youtube Tutorials,11,
ltr
item
A Passionate Developers Creation: Remove duplicates from Sorted Array - Leetcode - 26 - Java Solution
Remove duplicates from Sorted Array - Leetcode - 26 - Java Solution
Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_TCR_SsmR8v-pmtcJzD3dX8AECVDwtRcDSX94BEyyi28RZAep2wa8kqfbMk56dAvmJRvpaqXzaKQi-nlGKPZNUOOfw9G5vXlF8DMfKr4ztsBlUZ88kKwY4iHecdP49OOYD_kxiPc7Br4hcvt8yzZOBschey-6PTxSMb80uAEcXo3JAFDMiHXblmYWHFA/w200-h134/chris-ried-ieic5Tq8YMk-unsplash.jpg
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_TCR_SsmR8v-pmtcJzD3dX8AECVDwtRcDSX94BEyyi28RZAep2wa8kqfbMk56dAvmJRvpaqXzaKQi-nlGKPZNUOOfw9G5vXlF8DMfKr4ztsBlUZ88kKwY4iHecdP49OOYD_kxiPc7Br4hcvt8yzZOBschey-6PTxSMb80uAEcXo3JAFDMiHXblmYWHFA/s72-w200-c-h134/chris-ried-ieic5Tq8YMk-unsplash.jpg
A Passionate Developers Creation
https://www.apdevc.com/2024/10/remove-duplicates-from-sorted-array.html
https://www.apdevc.com/
https://www.apdevc.com/
https://www.apdevc.com/2024/10/remove-duplicates-from-sorted-array.html
true
3690156770086819421
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content