How to find duplicates in a string

Question:

I am working on creating a world game, and I do not want my game to accept duplicate input’s by the user, ex: ‘mirror’.

I tried finding the duplicates one way but it didn’t work, and I do not know how to make my program realize that the inout is a duplicate.

this is my non working code:

def checkForDuplicates(userWord):
 


    duplicates=[]

    
            return True
        

I tried setting this statement to true, if any duplicates where found but it didn’t work, I was expecting the function to check the users word for duplicate letters, and If there are duplicates the function returns True if not the function returns False.

this is also python programming language


Asked By: user93000

||

Answers:

The issue here is your return statement is nested inside of your for loop so it will always return after checking the first letter. You could move the return into an else statement so that it returns True after the first duplicate letter is found and then put return False after it gets all the way through the for loop without hitting any duplicates. (without the return False it would return None which also is a falsey value)

  • note: I changed the capitalization to fit Python standards

Here is a working runnable example

#!/usr/bin/env python

def check_for_duplicates(user_word):
    duplicates=[]
    for dup in user_word:
        if dup not in duplicates:
            duplicates.append(dup)
        else:
            return True
    return False

print(f'{check_for_duplicates("mirror")=}')
print(f'{check_for_duplicates("marine")=}')

<script src="https://modularizer.github.io/pyprez/pyprez.min.js" theme="darcula"></script>

Answered By: Modularizer

As you want to know if there are repeating characters in a string, we can loop through each character of the string and check if there is another duplicate character by using the string count(c) function.

I have made the code you need:

def checkForDuplicates(userWord):
    for char in userWord:
        if userWord.count(char) > 1:
            return True

    return False
Answered By: Nic13Gamer

Other answers already identified the problem in the loop. alternatively, if you want to try the set() function call.

This might be a more simple more optimized one because set() is faster than finding it running a python loop.

def has_duplicates(string):
    return len(set(string)) < len(string)

Output:

print(has_duplicates("Mirror"))  # True
print(has_duplicates("Hello world"))  # True
print(has_duplicates("Helo"))  # False
Answered By: Faisal Nazik

You are making some pretty simple mistakes.

Return True should be in an else statement instead of the if statement. Also, you need the program to return False if the for loop ends and True hasn’t been returned.

You could implement these suggestions like this:

def checkForDuplicates(userWord):
    duplicates = []
    for dup in userWord:
        if dup not in duplicates:
            duplicates.append(dup)
        else:     
            return True
    return False

Output:

print(checkForDuplicates("mirror")) # returns true
print(checkForDuplicates("taco")) # returns false

Edit: Modularizer also came to the exact same answer I did.

This returns true if given "mirror" and false if given "cake". I checked for a bunch of other values, and this is working properly.

Better Implementation Method

In my above code, I only modified your code, but you would be better off doing in differently.

Like user Faisal Nazik suggested, all you have to do is:

def has_duplicates(string):
    return len(set(string)) < len(string)
Answered By: Dylan Headley