xxxxxxxxxx
#finds occurances
def duplicatecharacters(s:str):
for i in s:
if s.count(i)>1:
return True
return False
print(duplicatecharacters(""))
xxxxxxxxxx
# count the occurence of element in the string
l=input("enter the string")
d={}
print(l)
for i in l:
if i not in d:
d[i]=l.count(i)
else:
pass
print("Frequency of each element-")
for k in d:
print(k,"-",d[k])
output:
'''
enter the string14587324569248
14587324569248
Frequency of each element-
1 - 1
4 - 3
5 - 2
8 - 2
7 - 1
3 - 1
2 - 2
6 - 1
9 - 1
'''
xxxxxxxxxx
string = "Hello world!"
character = "l"
occurrence = string.count(character) # give 3
xxxxxxxxxx
def count_occurrences(string, character):
count = 0
for char in string:
if char == character:
count += 1
return count
# Example usage
my_string = "Hello, World!"
my_character = "o"
occurrence_count = count_occurrences(my_string, my_character)
print(f"The character '{my_character}' occurs {occurrence_count} times in the string.")
xxxxxxxxxx
function countOccurrences(str, char) {
const regex = new RegExp(char, 'g');
const matches = str.match(regex);
return matches ? matches.length : 0;
}
const inputStr = 'hello, world!';
const charToCount = 'o';
const count = countOccurrences(inputStr, charToCount);
console.log(count); // Outputs: 2
xxxxxxxxxx
c = Counter("thiiss wiill caalcullateeee theee numbeeer of characters")
# now when we print c, it will have the following data:
Counter({'e': 11, ' ': 6, 'l': 5, 'a': 5, 't': 4, 'i': 4, 'c': 4, 'h': 3, 's': 3, 'r': 3, 'u': 2, 'w': 1, 'n': 1, 'm': 1, 'b': 1, 'o': 1, 'f': 1})
# And now we can check the occurrence of each of the characters as follows:
count_e = c.get('e') # returns 11