Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create merge_tools.py #152

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Create merge_tools.py #152

wants to merge 1 commit into from

Conversation

madhuri7112
Copy link
Owner

No description provided.

def remove_dups(string):
newstr = []
for i in range(len(string)):
if string[i] not in string[:i]:
Copy link
Owner Author

@madhuri7112 madhuri7112 Jun 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are iterating through the entire string until ith character for every character so the time complexity of this loop is O(n^2) which is not ideal.

Instead, you can use a set to do this

def remove_dups(string):
   seen_characters = {}
   result = ""
   for i in range(len(string)):
      if  string[i] not in seen_characters:
           result = result + string[i]
      seen_characters.add(string[i])

The time complexity of this loop is only O(n) because it iterates through the string only once and look up in set is O(1) time operation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant