DSA cheatsheet

Syntax snippets and quick notes for revision.

DSA Introduction

function uniqueValues(values) {
  const seen = new Set();
  return values.filter((value) => {
    if (seen.has(value)) return false;
    seen.add(value);
    return true;
  });
}

Use this dsa introduction pattern when a DSA task needs a small, readable starting point.

Problem Solving Method

function uniqueValues(values) {
  const seen = new Set();
  return values.filter((value) => {
    if (seen.has(value)) return false;
    seen.add(value);
    return true;
  });
}

Use this problem solving method pattern when a DSA task needs a small, readable starting point.

Time Complexity

function uniqueValues(values) {
  const seen = new Set();
  return values.filter((value) => {
    if (seen.has(value)) return false;
    seen.add(value);
    return true;
  });
}

Use this time complexity pattern when a DSA task needs a small, readable starting point.

Space Complexity

function uniqueValues(values) {
  const seen = new Set();
  return values.filter((value) => {
    if (seen.has(value)) return false;
    seen.add(value);
    return true;
  });
}

Use this space complexity pattern when a DSA task needs a small, readable starting point.

Big O Tradeoffs

function uniqueValues(values) {
  const seen = new Set();
  return values.filter((value) => {
    if (seen.has(value)) return false;
    seen.add(value);
    return true;
  });
}

Use this big o tradeoffs pattern when a DSA task needs a small, readable starting point.

Arrays

function uniqueValues(values) {
  const seen = new Set();
  return values.filter((value) => {
    if (seen.has(value)) return false;
    seen.add(value);
    return true;
  });
}

Use this arrays pattern when a DSA task needs a small, readable starting point.

Strings

function uniqueValues(values) {
  const seen = new Set();
  return values.filter((value) => {
    if (seen.has(value)) return false;
    seen.add(value);
    return true;
  });
}

Use this strings pattern when a DSA task needs a small, readable starting point.

Objects and Hash Maps

function uniqueValues(values) {
  const seen = new Set();
  return values.filter((value) => {
    if (seen.has(value)) return false;
    seen.add(value);
    return true;
  });
}

Use this objects and hash maps pattern when a DSA task needs a small, readable starting point.

Sets and Frequency Counting

function uniqueValues(values) {
  const seen = new Set();
  return values.filter((value) => {
    if (seen.has(value)) return false;
    seen.add(value);
    return true;
  });
}

Use this sets and frequency counting pattern when a DSA task needs a small, readable starting point.

Prefix Sum

function uniqueValues(values) {
  const seen = new Set();
  return values.filter((value) => {
    if (seen.has(value)) return false;
    seen.add(value);
    return true;
  });
}

Use this prefix sum pattern when a DSA task needs a small, readable starting point.

Recursion

function uniqueValues(values) {
  const seen = new Set();
  return values.filter((value) => {
    if (seen.has(value)) return false;
    seen.add(value);
    return true;
  });
}

Use this recursion pattern when a DSA task needs a small, readable starting point.

Sorting

function uniqueValues(values) {
  const seen = new Set();
  return values.filter((value) => {
    if (seen.has(value)) return false;
    seen.add(value);
    return true;
  });
}

Use this sorting pattern when a DSA task needs a small, readable starting point.