You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on May 29, 2024. It is now read-only.
I agree that the time cmplx is not O(n) but i think the reason is not the comparison statement.
The line of list.remove(value) looks for the value in list while it is nested in a loop of n iters, so in worst case it will have a complexity of O(n^2).
I agree that the time cmplx is not O(n) but i think the reason is not the comparison statement.
The line of list.remove(value) looks for the value in list while it is nested in a loop of n iters, so in worst case it will have a complexity of O(n^2).
To my knowledge using the in keyword followed by a python list is O(n).
If it was followed a set though, it would have been constant time.
Anyways you are also correct the line below is itself O(n). I haven't paid attention to that.
Hi this is vishal!!
lets make it more simpler
here is the code
sample_case = [0, 0, 1, 1, 1, 2, 2, 3, 3, 4]
def make_distinct(values: list) -> list:
"""
Remove duplicate elements in an array inplace without creating new array.
"""
index = len(values) - 1
while index > 0:
if values[index] in values[:index]:
values.pop(index)
index -= 1
return values
if name == "main":
print(make_distinct(sample_case))
use while instead of for loop and finally o(n)
Sign up for freeto subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Details
I'm pretty sure remove_duplicates_list.p is not O(n) time complexity.
Because of this line:
DSA/algorithms/Python/arrays/remove_duplicates_list.py
Line 25 in d3c2184
The text was updated successfully, but these errors were encountered: