Typescript Set Reduce

Typescript Set Reduce

Why shouldn't you be able to reduce your set?

array.reduce()

The reduce() method for arrays is magic. From this single method, you can reproduce the .filter(), .map(), and other array methods.

Playground Examples

But what about Set?

The Set data object is fantastic for keeping track of unique values where order is not important. However, Set does not have a .reduce() method... until now! Your challenge is create a reduceSet() function that will simulate the array.reduce() method. The function signature is left to you but as a hint, it should be a generic function and the first argument should be the Set upon which the "reducing" will happen.

Implementation can be really easy if you just convert the Set to an array and use .reduce() ...

typescript
1return [...setArg].reduce(...);

For this, you should not convert the Set into an array! It is more challenging this way. :-)

One Solution

popularity
Electric
posted
Jan. 16, 2024