Looking to arrange an array by grouping items together? For example:
[1, 1, 0, 1, 0, 1, 0] => [1, 1, 0, 1, 1, 0, 0]
OR
[1, 1, 0, 1, 0, 1, 0] => [[1, 1], [0], [1, 1], [0, 0]]
In this scenario, the goal is to group 1s with a maximum group size of 2 items. The objective is to pair values without grouping all items together.
Wondering about the best approach for writing such a function? Consider using Reduce or Sort. A clean solution utilizing functional programming techniques is preferred, especially if it allows for better abstractions. Although JavaScript is primarily used, solutions in any language are welcome as long as the logic is clear.
Background information: Working with a list of photos that need to be re-ordered so that square photos (represented by 1
in the example) appear adjacent to each other.