Jump to content

Hey guys, I have to write a function that detects duplicates in a sequence. So given string or list of numbers or strings, the function returns True if the string has duplicate letters or if the list has duplicate numbers or strings. How do I go about doing this? I know there is a built in method to do this, but I can't use it. I'm not asking you to write this for me, just give me some ideas of the logic behind it. Thanks so much!

Link to comment
https://linustechtips.com/topic/461975-python-function-help/
Share on other sites

Link to post
Share on other sites

for each item in string/list check list from start to that item position

if find equal item, delete this item

go to that same place in list -1 (because you deleted that item you need to move back 1 spot so that you dont miss the next item)

back to beginning

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
https://linustechtips.com/topic/461975-python-function-help/#findComment-6199324
Share on other sites

Link to post
Share on other sites

Let's say you have a string, what would be the best way to check if the item occurs more than one time? You start with the first letter and check if exists by going one letter at a time from the beginning of string till the end (hint), if, going through the letters, you find it again then the function will return true, if not it will keep on going till the end of string. Then it picks the 2nd letter and go through the string again, then third... till n, which is the length of the string. I can give you the algorithm and you translate it to python if you want.

Link to comment
https://linustechtips.com/topic/461975-python-function-help/#findComment-6199431
Share on other sites

Link to post
Share on other sites

Let's say you have a string, what would be the best way to check if the item occurs more than one time? You start with the first letter and check if exists by going one letter at a time from the beginning of string till the end (hint), if, going through the letters, you find it again then the function will return true, if not it will keep on going till the end of string. Then it picks the 2nd letter and go through the string again, then third... till n, which is the length of the string. I can give you the algorithm and you translate it to python if you want.

I'm going to give it a try first and then I'll let you know if I need help. Thanks!!

Link to comment
https://linustechtips.com/topic/461975-python-function-help/#findComment-6199462
Share on other sites

Link to post
Share on other sites

Two ways I can think of to approach this.  Without some testing I couldn't tell you which would be better (faster/less memory intensive/better scaling with large lists and strings/etc), but both should be easy to use.

 

  • For the whole list, convert it to a set (which contains only the unique elements of the list), and see if the set('s length) is the same as the list('s length).  If not, then you have duplicates.  Do the same thing for each string--set(str) returns a set of each unique character in the string.
  • Or, using the count() method.  X.count(Y) returns the number of times Y occurs in X.  This works for both substrings in a string and items in a list.
Link to comment
https://linustechtips.com/topic/461975-python-function-help/#findComment-6199864
Share on other sites

Link to post
Share on other sites

Let's say you have a string, what would be the best way to check if the item occurs more than one time? You start with the first letter and check if exists by going one letter at a time from the beginning of string till the end (hint), if, going through the letters, you find it again then the function will return true, if not it will keep on going till the end of string. Then it picks the 2nd letter and go through the string again, then third... till n, which is the length of the string. I can give you the algorithm and you translate it to python if you want.

Hey I could use some help...haha I got it to cycle through the string, but when I'm comparing letters, I can't figure out how to negate the letter that I've already read in. So for example, If I have the word 'hello', when I read in h, I can't get it to compare it only to the other letters. I don't know if that makes sense.....

Link to comment
https://linustechtips.com/topic/461975-python-function-help/#findComment-6199927
Share on other sites

Link to post
Share on other sites

Hey I could use some help...haha I got it to cycle through the string, but when I'm comparing letters, I can't figure out how to negate the letter that I've already read in. So for example, If I have the word 'hello', when I read in h, I can't get it to compare it only to the other letters. I don't know if that makes sense.....

 

You should always start with the next letter. So if you started your loop from i=0, your nested loop should start from the i position + 1

Link to comment
https://linustechtips.com/topic/461975-python-function-help/#findComment-6200067
Share on other sites

Link to post
Share on other sites

You could create a hash table.

It would be O(n) on average, but worst case can be O(n2) I think.

 

Or you can use sets , which would have O(n log n) worst case.

 

Or you can make a copy of your list/vector/array/whatever, sort it, then check adjacent elements. A decent sort tehnique would require O(n log n) for comparing then n-1 for checking adjecent elements.

 

Or, make another list, then go through your original list, adding each element if they aren't found anywhere in the new list.

 

Of course, all of these that I talked about could be used to get rid of duplicates. Checking for duplicates should be easier.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
https://linustechtips.com/topic/461975-python-function-help/#findComment-6200182
Share on other sites

Link to post
Share on other sites

You should always start with the next letter. So if you started your loop from i=0, your nested loop should start from the i position + 1

def has_duplicates(xs):	duplicates = False		for x in xs:	    for y in xs[index[x + 1]]:                if x == y:	                    duplicates = True				return duplicates

This is what I have so far....the index function only works on lists and I'm not necessarily always going to be entering a list. Is my logic correct??

Link to comment
https://linustechtips.com/topic/461975-python-function-help/#findComment-6200654
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×