Home > OS >  Removing duplicates using a dictionary
Removing duplicates using a dictionary

Time:10-07

I am writing a function that is supposed to count duplicates and mention how many duplicates are of each individual record. For now my output is giving me the total number of duplications, which I don't want.

i.e. if there are 4 duplicates of one record, it's giving me 4 instead of 1; if there are 6 duplicates of 2 individual records it should give me 2.

Could someone please help find the bug?

Thank you

def duplicate_count(text):
text = text.lower()
dict = {}
word = 0
if len(text) != "":
    for a in text:
        dict[a] = dict.get(a,0)   1
    for a in text:
        if dict[a] > 1:                
            word = word   1
    return word
else:
    return "0"
        
            
            

CodePudding user response:

Fixed it:

def duplicate_count(text):
text = text.lower()
dict = {}
word = 0
if len(text) != "":
    for a in text:
        dict[a] = dict.get(a,0)   1
    return sum(1 for a in dict.values() if a >= 2) 
else:
    return "0"
        

CodePudding user response:

You can do this with set and sum. First set is used to remove all duplicates. This is so we can have as few iterations as possible, as-well-as get an immediate count, as opposed to a "one-at-a-time" count. The set is then used to create a dictionary that stores the amount of times a character repeats. Those values are then used as a generator in sum to sum all the times that the "repeat value" is greater than 1.

def dup_cnt(t:str) -> int:
    if not t: return 0

    t = t.lower()
    d = dict()
    
    for c in set(t): 
        d[c] = t.count(c)

    return sum(v>1 for v in d.values())

print(dup_cnt('aabccdeefggh')) #4
  • Related