Existing solution is a bit overkill (reduce + temp array). This PR introduces, I believe, a simpler to understand solution.