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.2 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
691b559495c5cb5a37b9b481 Challenge 121: Most Frequent 28 challenge-121

--description--

Given an array of elements, return the element that appears most frequently.

  • There will always be a single most frequent element.

--hints--

mostFrequent(["a", "b", "a", "c"]) should return "a".

assert.equal(mostFrequent(["a", "b", "a", "c"]), "a");

mostFrequent([2, 3, 5, 2, 6, 3, 2, 7, 2, 9]) should return 2.

assert.equal(mostFrequent([2, 3, 5, 2, 6, 3, 2, 7, 2, 9]), 2);

mostFrequent([true, false, "false", "true", false]) should return false.

assert.isFalse(mostFrequent([true, false, "false", "true", false]));

mostFrequent([40, 20, 70, 30, 10, 40, 10, 50, 40, 60]) should return 40.

assert.equal(mostFrequent([40, 20, 70, 30, 10, 40, 10, 50, 40, 60]), 40);

--seed--

--seed-contents--

function mostFrequent(arr) {
  return arr;
}

--solutions--

function mostFrequent(arr) {
  const freq = {};
  let maxCount = 0;
  let mostElem = null;

  for (const el of arr) {
    freq[el] = (freq[el] || 0) + 1;
    if (freq[el] > maxCount) {
      maxCount = freq[el];
      mostElem = el;
    }
  }

  return mostElem;
}