Remove Duplicates from Sorted Array - Leetcode 26 - Python

Remove Duplicates from Sorted Array - Leetcode 26 - Python

NeetCode

2 года назад

160,742 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Butoyi Ghyslain
Butoyi Ghyslain - 05.11.2023 07:22

did this on my own after checking your other vids, thanks to you Neetcode!

Ответить
Riccelli Marques
Riccelli Marques - 02.11.2023 19:00

Can we say that it is some way a sliding window?

Ответить
Fahad Ahmed Akash
Fahad Ahmed Akash - 27.10.2023 02:14

why using set gives wrong answer?
it prints the correct value in stdout but wrong in output

Ответить
Dhruv parmar
Dhruv parmar - 19.10.2023 05:00

def removeDuplicate(n):
l = 1
for r in range(1, len(n)):
if n[r] != n[l]:
l += 1
n[l] = n[r]

return l+1

Ответить
Famelhaut
Famelhaut - 07.10.2023 20:37

This problem is written like shit. This comment helped solve it without watching the solution:

"They don't really want you to remove the duplicates. They want you to sort the uniques at the front, then return the length of the sorted part. Then, behind the scenes, they slice the array at the length you give them and the result of that is what they check.

Just FYI, this sh_t drove me crazy..."

Ответить
JohnEdits
JohnEdits - 27.09.2023 17:32

If input is [1,1,2] it will do [1,2,2] and l=2
So print [1,2]
But expected output is [1,2,_]
To do this you need to create new array

Ответить
JohnEdits
JohnEdits - 27.09.2023 17:30

It not delete just reassign value

Ответить
Abhishek Yadav
Abhishek Yadav - 18.09.2023 16:59

if array like [0 1 2 2 3] and we are starting from index 1 then r # r+1 so we lose the value 1 cuz 2#1, can someone explain me pls

Ответить
Abner Ramirez
Abner Ramirez - 13.09.2023 22:27

when i put the same code you did I still didn't get the write answer

Ответить
Su Myat Noe
Su Myat Noe - 05.09.2023 10:35

In my opinion, how about we used sort function? Answer : unique_arr = list(set(arr))

Ответить
SAB5106
SAB5106 - 30.08.2023 02:27

Why is this code not working for the solution?

class Solution:
def removeDuplicates(self, nums):
uniqueset = set()
for n in nums:
uniqueset.add(n)

return len(uniqueset)

I've tried this outside of LeetCode's checkers and it returns an integer of the expected length. Doing print(uniqueset) returns a set of the expected values, but Leetcode's checker says it returns a completely different list of values. I'm very confused as to what has gone wrong, but I am new to Python3.

Ответить
Atalay Karahan
Atalay Karahan - 23.08.2023 11:24

i did not understand when i try to use hash set and return the count of hash leetcode return me a error can we solve this problem with hash set? if its not why?

Ответить
Joel Rhine
Joel Rhine - 17.08.2023 14:06

My test cases were accepted but my final submission didn’t get accepted because it exceeded the time limit. That was just a clear reflection of how I code, inefficient but getting the job done. My house is burning but hey, at least we got heat.

Ответить
T-distributed Kid
T-distributed Kid - 28.07.2023 23:24

Having a third pointer specifically for maintaing the unique value is much easier and more understandable thhan managing with two pointers

Ответить
Deekshith TN
Deekshith TN - 15.07.2023 08:24

No one can replace you❤

Ответить
bboyrusk
bboyrusk - 09.07.2023 18:39

ОХуенно!

Ответить
Woody
Woody - 02.07.2023 15:01

The LeetCode instructions were absolutely awful for this question. They rarely seem to explain stuff in simple terms, it's so annoying. Half the battle is deciphering the question itself, sometimes the code isn't even hard to come up with, just interpreting the question.

Ответить
Kevin Liu
Kevin Liu - 29.06.2023 06:49

Thank you! I was a bit confused because I thought you had to null all the other values in the updated array, because the question had _ representing those indices.

Ответить
Tutorial Techie
Tutorial Techie - 28.06.2023 12:55

public int removeDuplicates(int[] nums) {
int k = 1; // k is the left pointer
for (int i = 1; i < nums.length; i++) { // ? i is the right pointer
if (nums[i] != nums[i - 1])
nums[k++] = nums[i];
}
return k;
}

Ответить
Mohamed El Hadi
Mohamed El Hadi - 28.05.2023 22:38

I came up with a crafty solution xD, but it's in O(n^2) !

Ответить
Aadityakiran S
Aadityakiran S - 02.05.2023 06:42

About as efficient as it can get huh? But the LeetCode statistics aren't that reliable in terms of efficiency right?

Ответить
Susama Biswas
Susama Biswas - 14.04.2023 10:10

I actually spend so much time solving it.. that too by shifting the elements but it could have done a much simpler way. I really don't know if I will ever get good enough to reach the point where the best solution just clicks after some thinking .it's so frustrating

Ответить
Rohit Kumar Jha
Rohit Kumar Jha - 07.04.2023 09:41

anyone could please send the program that i could execute on my ide with using the same logic

Ответить
Vasylyna Pavelko
Vasylyna Pavelko - 22.03.2023 13:47

Really like your explanations. I'm using JavaScript, but you make it so easy to undestand the problem and the solution for it.

Ответить
Saurabh Kumar Srivastava
Saurabh Kumar Srivastava - 16.03.2023 18:11

Great man salute 😄nicely explained

Ответить
Erica Metta
Erica Metta - 22.01.2023 08:05

The array cant be empty cuz the first constraint in the question is (1 <= nums.length <= 3 * 104)

Ответить
Black Playdoh
Black Playdoh - 04.01.2023 10:19

I hated this problem

Ответить
Dennis Tsai
Dennis Tsai - 29.12.2022 12:09

You just made it so easy!

Ответить
Joshua Duffney
Joshua Duffney - 24.12.2022 16:22

"Return the number of unique values in the array" is SO much better than saying "Return k", thank you!

Ответить
Rahul Datta
Rahul Datta - 20.12.2022 20:23

can someone please tell me what argument is -> int is

Ответить
nathan_milan_blacksmith
nathan_milan_blacksmith - 09.12.2022 20:18

hi, what drawing app are you using? thanks

Ответить
davide
davide - 28.11.2022 20:21

i don't understand why we can't use the pop function

Ответить
Fariba Tasnia Khan
Fariba Tasnia Khan - 23.11.2022 13:47

Oh my God... What an easy solution... And i was thinking all the complex way I can

Ответить
Maamoun Haj Najeeb
Maamoun Haj Najeeb - 09.11.2022 12:20

thanks

Ответить
What Streamer?
What Streamer? - 27.10.2022 19:34

easy with numbers what about strings ?

Ответить
Elizabeth R
Elizabeth R - 13.10.2022 11:40

Such a clean and brilliant solution.

Ответить
Nithin Prasad
Nithin Prasad - 06.10.2022 23:21

Java solution:

public static int removeDuplicates(int[] arr) {
int left = 1;

for (int right = 1; right < arr.length; right++) {
if (arr[right] != arr[right - 1]) {
arr[left] = arr[right];
left++;
}
}
return left;
}

Ответить
Sai Aung Kyaw Maw
Sai Aung Kyaw Maw - 29.09.2022 23:54

Does anyone have problem running this code with zero or negative value's array on leet code simulation? Whenever i run array that has zero or negative value, the loop left out the very last value.

Ответить
Cider Bizet
Cider Bizet - 29.09.2022 01:50

Problem seems to be very simple, but also seems to be written by an alien

Ответить
Amal Deepak Thiru
Amal Deepak Thiru - 21.09.2022 21:55

Why can't we just convert it to a set, and then convert it to a set and then return the length of the set?

Ответить
Tsukuyomi
Tsukuyomi - 30.08.2022 00:05

i put the same codes but its saying theres an error

Ответить
Nishant Sharma
Nishant Sharma - 27.08.2022 21:49

def removeDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
x = 0
while x < len(nums):
val = nums[x]
if nums[x+1:].count(val) >= 1:
# nums.remove(val)
nums[x+1:] = list(filter(lambda a: a != val, nums[x+1:]))
else:
x += 1
return len(nums)

How is this approach?

Ответить
Ali Ahmad
Ali Ahmad - 16.08.2022 04:53

not sure why i get an index out of bound getting the same output using golang

Ответить
vinothini seenivasan
vinothini seenivasan - 13.08.2022 22:45

thanks for good work...crystal clear explanation..

Ответить
M Idrees
M Idrees - 13.08.2022 18:48

def removeDuplicates(self, nums: List[int]) -> int:

prev = None

j=0
for i in nums:
if i != prev:
nums[j] = i
j+=1
prev = i

return j

Ответить
Fatima Darbandsari
Fatima Darbandsari - 31.07.2022 11:48

Thank you

Ответить