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
#import the Counter class from collections module
from collections import Counter
#An iterable with elements to count
data = 'aabbbccccdeefff'
#create the Counter object
c = Counter(data)
print(c)
#get the count of a specific element
print(c['f'])
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
#finds occurances
def duplicatecharacters(s:str):
for i in s:
if s.count(i)>1:
return True
return False
print(duplicatecharacters(""))