Python Forum
Problem : Count the number of Duplicates
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem : Count the number of Duplicates
#1
Problem : 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

def duplicate_count(text):
    new_list = []
    list_to_set = set(new_list)
    for i in text:
      new_list.append(i)
      if len(new_list) != len(list_to_set):
        print("Difference")
    return new_list
test = duplicate_count("665")
Im kind of stuck how to solve this in python. I think my thought process is correct but im not executing it correctly.. What I did is first I make a empty list and then I convert it into the set and try to compare the differences from there. I see all the solutions to the problem using a dictionary when I searched up the problem but I want it to be completed using a set/list. Can someone help me on this? I know my code isn't correct
Reply
#2
I recommend that you go with the dictionary. I think your going to find that there will be a lot of trouble trying to index a set. Here's one way to go about it using a dictionary.
def duplicate_count (text) :
	total = 0
	duplicates = {}
	for character in text.lower () :
		if character in duplicates :
			duplicates [character] += 1
			if duplicates [character] < 3 :
				total += 1
		else :
			duplicates [character] = 1

	print (f'\nThere are {total} reoccurring characters.')
	for element in duplicates :
		if duplicates [element] > 1 :
			print (f'\t"{element}" occurs {duplicates [element]} times.')

duplicate_count ('abCccdeeEEefgg12334')
Reply
#3
(Dec-16-2021, 02:36 AM)BashBedlam Wrote: I recommend that you go with the dictionary. I think your going to find that there will be a lot of trouble trying to index a set. Here's one way to go about it using a dictionary.
def duplicate_count (text) :
	total = 0
	duplicates = {}
	for character in text.lower () :
		if character in duplicates :
			duplicates [character] += 1
			if duplicates [character] < 3 :
				total += 1
		else :
			duplicates [character] = 1

	print (f'\nThere are {total} reoccurring characters.')
	for element in duplicates :
		if duplicates [element] > 1 :
			print (f'\t"{element}" occurs {duplicates [element]} times.')

duplicate_count ('abCccdeeEEefgg12334')

Hello, sorry to post here but I've tried to PM you and seems like you cannot receive them. I'd like to get in touch with you regarding some project, how can I? Thank you
Reply
#4
I see two ways using modules from the standard library, the first uses class collections.Counter
from collections import Counter

def duplicate_counts(text):
    return sum(v > 1 for v in Counter(text.lower()).values())
The other one uses function re.findall()
import re

def duplicate_counts(text):
    return len(re.findall(r'(.)\1+', ''.join(sorted(text.lower()))))
Here is a third way using module numpy
import numpy as np

def duplicate_counts(text):
    x = np.unique(np.fromiter(text.lower(), '|S1'), return_counts=True)[1]
    return np.count_nonzero(x > 1)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  remove partial duplicates from csv ledgreve 0 797 Dec-12-2022, 04:21 PM
Last Post: ledgreve
  Row Count and coloumn count Yegor123 4 1,342 Oct-18-2022, 03:52 AM
Last Post: Yegor123
  Problem with "Number List" problem on HackerRank Pnerd 5 2,128 Apr-12-2022, 12:25 AM
Last Post: Pnerd
  Unable to count the number of tries in guessing game. Frankduc 7 1,928 Mar-20-2022, 08:16 PM
Last Post: menator01
  number accuracy problem? roym 5 1,857 Dec-24-2021, 07:57 AM
Last Post: roym
  Removal of duplicates teebee891 1 1,805 Feb-01-2021, 12:06 PM
Last Post: jefsummers
  Count number of occurrences of list items in list of tuples t4keheart 1 2,399 Nov-03-2020, 05:37 AM
Last Post: deanhystad
  Displaying duplicates in dictionary lokesh 2 1,999 Oct-15-2020, 08:07 AM
Last Post: DeaD_EyE
  P3, openpyxl, csv to xlsx, cell is not number, problem with colorize genderbee 1 2,168 Sep-29-2020, 03:20 PM
Last Post: Larz60+
  Count number of values Kristenl2784 0 1,522 Jul-16-2020, 01:26 PM
Last Post: Kristenl2784

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020