Skip to content

Dictionary

Key-Value storage — Like a real dictionary: look up a word (key), get its definition (value)


Concept

Real-Life Analogy

** Phone book**: You look up a person's name (key) and find their phone number (value). You don't need to remember which page they're on — just search by name.

More examples:

  • Configuration: Setting name (key) → setting value (value)
  • ID card: ID number (key) → personal info (value)
  • Cache: URL (key) → cached response (value)

Array vs Dictionary

PropertyArrayDictionary
IndexNumeric (0, 1, 2...)Any type (string, number, object)
LookupBy index or loopDirectly by key
Use caseOrdered listsKey-value mapping

Code

javascript
class Dictionary {
  constructor() {
    this.items = {}
  }

  // Set a key-value pair
  set(key, value) {
    this.items[key] = value
  }

  // Check if a key exists
  has(key) {
    return key in this.items
  }

  // Remove a key-value pair
  remove(key) {
    if (!this.has(key)) return false
    delete this.items[key]
    return true
  }

  // Get a value by key
  get(key) {
    return this.has(key) ? this.items[key] : undefined
  }

  // Get all keys
  keys() {
    return Object.keys(this.items)
  }

  // Get all values
  values() {
    return Object.values(this.items)
  }

  // Get the number of items
  size() {
    return Object.keys(this.items).length
  }

  // Clear everything
  clear() {
    this.items = {}
  }
}

Key Points

  1. Wraps a JS objectthis.items = {} uses JavaScript's built-in key-value feature
  2. key in object checks existence — more reliable than obj[key] !== undefined
  3. ** Gotcha**: Object keys are always strings. set(1, 'a') and set('1', 'a') will overwrite each other!

Complexity

OperationTimeNotes
set()O(1)Property assignment
get()O(1)Property access
has()O(1)in operator
remove()O(1)delete operator

Interview Questions

1. Two Sum

Given an array of numbers and a target, find two numbers that add up to the target.

javascript
function twoSum(nums, target) {
  const dict = new Dictionary()

  for (let i = 0; i < nums.length; i++) {
    const complement = target - nums[i]

    if (dict.has(complement)) {
      return [dict.get(complement), i]
    }
    dict.set(nums[i], i)
  }
  return []
}

console.log(twoSum([2, 7, 11, 15], 9)) // [0, 1]

Summary

PropertyDescription
Core ideaKey-value mapping
Key opsset (store), get (retrieve), has (check)
TimeAll core ops O(1)
LimitationKeys become strings automatically
NextHash Table — A faster, hand-crafted Dictionary

LeetCode Practice

#ProblemDifficulty
1Two SumEasy
242Valid AnagramEasy
49Group AnagramsMedium

Back to Priority Queue · Back to Home

MIT Licensed | Made with ❤️ for JS learners