Skip to content
This repository has been archived by the owner on May 29, 2024. It is now read-only.

Inaccuracy in file remove_duplicates_list.py #1249

Open
Mahyar-GH opened this issue Sep 12, 2023 · 4 comments
Open

Inaccuracy in file remove_duplicates_list.py #1249

Mahyar-GH opened this issue Sep 12, 2023 · 4 comments
Labels
bug Something isn't working

Comments

@Mahyar-GH
Copy link

Details
I'm pretty sure remove_duplicates_list.p is not O(n) time complexity.
Because of this line:

if values[index_position] in values[0:index_position]:

@Mahyar-GH Mahyar-GH added the bug Something isn't working label Sep 12, 2023
@welcome
Copy link

welcome bot commented Sep 12, 2023

Thanks for opening your first issue here! Be sure to follow the issue template!

@noor-malaika
Copy link

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).

@Mahyar-GH
Copy link
Author

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.

@vishalkhyatriya12
Copy link

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 free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants