Files
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

1.9 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
69e2383af7832c8032603b91 Challenge 275: Character Frequency 28 challenge-275

--description--

Given a string, return an object (JavaScript) or dictionary (Python) mapping each character to the number of times it appears.

--hints--

getFrequency("test") should return {t: 2, e: 1, s: 1}.

assert.deepEqual(getFrequency("test"), {t: 2, e: 1, s: 1});

getFrequency("mississippi") should return {m: 1, i: 4, s: 4, p: 2}.

assert.deepEqual(getFrequency("mississippi"), {m: 1, i: 4, s: 4, p: 2});

getFrequency("hello world") should return {h: 1, e: 1, l: 3, o: 2, " ": 1, w: 1, r: 1, d: 1}.

assert.deepEqual(getFrequency("hello world"), {h: 1, e: 1, l: 3, o: 2, " ": 1, w: 1, r: 1, d: 1});

getFrequency("She sells seashells by the seashore.") should return {S: 1, h: 4, e: 7, " ": 5, s: 7, l: 4, a: 2, b: 1, y: 1, t: 1, o: 1, r: 1, ".": 1}.

assert.deepEqual(getFrequency("She sells seashells by the seashore."), {S: 1, h: 4, e: 7, " ": 5, s: 7, l: 4, a: 2, b: 1, y: 1, t: 1, o: 1, r: 1, ".": 1});

getFrequency("The quick brown fox jumps over the lazy dog.") should return {T: 1, h: 2, e: 3, " ": 8, q: 1, u: 2, i: 1, c: 1, k: 1, b: 1, r: 2, o: 4, w: 1, n: 1, f: 1, x: 1, j: 1, m: 1, p: 1, s: 1, v: 1, t: 1, l: 1, a: 1, z: 1, y: 1, d: 1, g: 1, ".": 1}.

assert.deepEqual(getFrequency("The quick brown fox jumps over the lazy dog."), {T: 1, h: 2, e: 3, " ": 8, q: 1, u: 2, i: 1, c: 1, k: 1, b: 1, r: 2, o: 4, w: 1, n: 1, f: 1, x: 1, j: 1, m: 1, p: 1, s: 1, v: 1, t: 1, l: 1, a: 1, z: 1, y: 1, d: 1, g: 1, ".": 1});

--seed--

--seed-contents--

function getFrequency(str) {

  return str;
}

--solutions--

function getFrequency(str) {
  return str.split('').reduce((acc, char) => {
    acc[char] = (acc[char] || 0) + 1;
    return acc;
  }, {});
}