DEV Community

Lancelot03
Lancelot03

Posted on

3 3

Counting Duplicates

DESCRIPTION:
Count the number of Duplicates
Write a function that will return the count of distinct case-insensitive alphabetic characters and numeric digits that occur more than once in the input string. The input string can be assumed to contain only alphabets (both uppercase and lowercase) and numeric digits.

Example:

"abcde" -> 0 # no characters repeats more than once
"aabbcde" -> 2 # 'a' and 'b'
"aabBcde" -> 2 # 'a' occurs twice and 'b' twice (`b` and `B`)
"indivisibility" -> 1 # 'i' occurs six times
"Indivisibilities" -> 2 # 'i' occurs seven times and 's' occurs twice
"aA11" -> 2 # 'a' and '1'
"ABBA" -> 2 # 'A' and 'B' each occur twice
Enter fullscreen mode Exit fullscreen mode

Solution- ###Python

def duplicate_count(text):
    if len(text) == 0:
        return 0
    text=text.lower()
    count=0
    for i in set(text):
        if text.count(i)>1:
            count +=1
    return count
Enter fullscreen mode Exit fullscreen mode

Top comments (4)

Collapse
 
sophist profile image
Sophist

How about:

def duplicate_count(text):
    if len(text) == 0:
        return 0
    text=text.lower()
    return sum(map(lambda x: text.count(x) > 1,set(text)))
Enter fullscreen mode Exit fullscreen mode
Collapse
 
lancelot03 profile image
Lancelot03

It was good but difficult to understand for beginners.

Collapse
 
farrukh007 profile image
Farrukh

Hi...do u know ML programming ?

Collapse
 
lancelot03 profile image
Lancelot03 • Edited

Yhaa I right now learning Machine Learning with python

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay