Deep Equality Check

MediumUtility Functions20 min4 tests

Implement an isEqual function that performs a deep equality comparison between two values.

Requirements

  • Handle primitives (number, string, boolean, null, undefined)
  • Recursively compare objects and arrays
  • Two objects are equal if they have the same keys with deeply equal values
  • Two arrays are equal if they have the same length and deeply equal elements

Example

isEqual({ a: { b: 1 } }, { a: { b: 1 } }); // true
isEqual([1, [2, 3]], [1, [2, 3]]);           // true
isEqual({ a: 1 }, { a: 2 });                 // false
Hints (4)
  1. Start with the base case: if both values are strictly equal (===), return true.
  2. Check the types of both values. If they differ, return false.
  3. For arrays, compare lengths first, then recursively compare each element.
  4. For objects, compare the sets of keys, then recursively compare each value.

Topics

  • Utility Functions

Asked at

Meta · Google · Uber · Airbnb

Join Us
blur