Remove Duplicates from Sorted Array | Leetcode 26 | Top 150 interview question series

preview_player
Показать описание
Top 150 interview question series
Remove Duplicates from Sorted Array
Leetcode problem number 26

JAVA interview programming playlist:

Git Repo:
Рекомендации по теме
Комментарии
Автор

1) HashSet could be used
2) the condition for upper bound of i in if block was unnecessary. You could've said: i<nums.length-1 in the for block too.
3) No need of making unnecessary if blocks if all you did was continue. You could've made an if block just for if(nums[i]!=nums[i+1])
Java would've continued on its own.

Thank you

saarza
Автор

class Solution {
public int removeDuplicates(int[] nums) {
int k = 1;
for (int i = 1; i < nums.length; i++) {
if (nums[i] != nums[k-1]) {
nums[k] = nums[i];
k++;
}
}
return k;
}
}

musamehdiyevv
Автор

mam pls continue this interview series thanks you mam🤩🤩🤩🤩

AjeetTiwari
Автор

great explanation simple approach for those in new in dsa

PranaySedam-ny
Автор

why are you writing i<nums.length-1 in if loop again? cant we just write in for loop itself

ShivaKumar-vusg
Автор

can u continue the series mam pls
ur approach is so good

vamshisundupalle
Автор

let iteration=0
let counter=0
While(iteration<nums.length){

counter++
nums[counter]=nums[iteration]
}
else{
nums[counter]=nums[iteration]
}
iteration++
}

Return counter

rajatchauhan
Автор

Hi, Could you please help me in understanding this question, because I have done this problem in other way, I am getting the desired result in IDE but in leetcode it is failing . Please have a look on below approach:

Set<Integer>
int[] expectedNums=new int[set.size()];
int index=0;
for(int i:set){
expectedNums[index]=i;
index++;
}



I can return expectedNums.length at the end . But this solution is not accepted, why ?

subhashreesahoo
Автор

mam your code is left the last element always so return count is always less one

here is the write code
int pointer = 0;
for (int i=0; i<nums.length-1; i++){
if (nums[i] != nums[i+1]){
nums[++pointer] = nums[i+1];
}
}
return nums.length ==0 ? 0 : pointer+1;

narendrabudaniya
Автор

@6.42 Why the last element won't be the duplicate?

MuthumanipandiM
Автор

why to use (nums.length-1) in if condition. its working without this also. Submitted in leetcode and accepted successfully

working code:
var removeDuplicates = function (nums) {
let count = 0;
for (let i = 0; i < nums.length; i++) {
if (nums[i] === nums[i + 1]) {
continue;
} else {
nums[count] = nums[i];
count++;
}
}
return count;
};

RajYadav-yhvv
Автор

please explain this -> nums[count] = nums[i];

imvishu
Автор

Mam, can u provide these 150 questions list...

dipeshraj
Автор

Mam can you give me the solution in C language pls!

navinvenkat
Автор

plzz help me mujhse question solve hi nhi hote 😭😭😭😭😭 mai soch hi nhi pata kaise start kru code likhna

helloworld-nz