Skip to content
Hash map: count and look up

Module challenge

Challenge: hash map

20 min · 3 steps

  1. Step 1 of 3 · Write the code

    solution(nums, target)

    Return the indexes [i, j] of the two numbers in nums that add up to target, with i < j. You can't use the same number twice.

    Example: solution([2, 7, 11, 15], 9) returns [0, 1].

    Constraints:

    • 2 ≤ nums.length ≤ 10⁵
    • -10⁹ ≤ nums[i], target ≤ 10⁹
    • Exactly one answer exists
    function solution(nums, target) { }
  2. Step 2 of 3 · Write the code

    solution(s)

    Return the index of the first letter in s that appears exactly once. If there isn't one, return -1.

    Example: solution("loveleetcode") returns 2 (the letter "v").

    Constraints:

    • 0 ≤ s.length ≤ 10⁵
    • s only contains lowercase letters
    function solution(s) { }
  3. Step 3 of 3 · Write the code

    solution(nums)

    Return the number that appears most often in nums. On a tie, return the smallest of the tied numbers.

    Example: solution([4, 1, 4, 1, 2]) returns 1 (1 and 4 both appear twice, and 1 is smaller).

    Constraints:

    • 1 ≤ nums.length ≤ 10⁵
    • -10⁹ ≤ nums[i] ≤ 10⁹
    function solution(nums) { }