Question Bank

JavaScript Take-Out-and-Rest Array: Two Approaches Quiz

Difficulty: Medium

Two seeded approaches to build a map of each-element-to-the-rest (reduce + filter and forEach + filter), plus two companions on slice + concat and a complexity discussion.

Question Bank
/

JavaScript Take-Out-and-Rest Array: Two Approaches Quiz

JavaScript Take-Out-and-Rest Array: Two Approaches Quiz

Two seeded approaches to build a map of each-element-to-the-rest (reduce + filter and forEach + filter), plus two companions on slice + concat and a complexity discussion.

Question Bank
Medium
JavaScript
4 questions
quiz
arrays
array-manipulation-patterns
js-spread-rest

454 views

9

Implement getRestItems(arr) that returns an object whose keys are each element and whose value is the array with that element removed. Solve it with reduce and filter.

Examples

Example 1:

Input: [1, 2, 3, 4]
Output: { 1: [2,3,4], 2: [1,3,4], 3: [1,2,4], 4: [1,2,3] }
Explanation: Each key maps to the array with that value filtered out.

Example 2:

Input: ['a', 'b', 'c']
Output: { a: ['b','c'], b: ['a','c'], c: ['a','b'] }
Explanation: Same shape for strings; the keys become the property names.