Foundation·Concept·2 min read

Arrays and strings in JavaScript

Practice this question

Problems to practice

  • Two Sum
  • Move Zeroes
  • Best Time to Buy and Sell Stock
  • Maximum Subarray
  • Product of Array Except Self
  • Valid Palindrome
  • Longest Substring Without Repeating Characters
  • Group Anagrams

Example: two sum

JavaScript
function twoSum(nums, target) {
  const map = new Map();

  for (let i = 0; i < nums.length; i++) {
    const needed = target - nums[i];
    if (map.has(needed)) return [map.get(needed), i];
    map.set(nums[i], i);
  }

  return [];
}

Time and space complexity

  • Time: O(n)
  • Space: O(n)

Adapted from 16_DSA_JAVASCRIPT.md · “2. Arrays and strings in JavaScript”. Code examples are study fragments. Difficulty and core labels are editorial estimates; version-specific behavior should be checked against your project.