हमें एक जावास्क्रिप्ट फ़ंक्शन लिखना है जिसमें निम्न स्ट्रिंग शामिल है -
const str = 'This string will be used to calculate frequency distribution';
हमें एक ऐसी वस्तु वापस करने की आवश्यकता है जो सरणी में मौजूद विभिन्न तत्वों के आवृत्ति वितरण का प्रतिनिधित्व करती है।
उदाहरण
निम्नलिखित कोड है -
const str = 'This string will be used to calculate frequency
distribution';
const frequencyDistribution = str => {
const map = {};
for(let i = 0; i < str.length; i++){
map[str[i]] = (map[str[i]] || 0) + 1;
};
return map;
};
console.log(frequencyDistribution(str)); आउटपुट
कंसोल में आउटपुट निम्नलिखित है -
{
T: 1,
h: 1,
i: 6,
s: 4,
' ': 8,
t: 5,
r: 3,
n: 3,
g: 1,
w: 1,
l: 4,
b: 2,
e: 5,
u: 4,
d: 2,
o: 2,
c: 3,
a: 2,
f: 1,
q: 1,
y: 1
}