const result = arr
.reduce((acc, n, i, a) => (
a[i - 1] !== n && acc.push([ n, 0 ]),
acc[acc.length - 1][1]++,
acc
), [])
.map(([ v, c ]) => c === 1 ? v : Array(c).fill(v));
или
const result = arr
.reduce((acc, n, i, a) => (
a[i - 1] === n || acc.push([]),
acc[acc.length - 1].push(n),
acc
), [])
.map(n => n.length === 1 ? n[0] : n);
или
const result = arr.reduce((acc, n, i, a) => {
const prev = n === a[i - 1];
const next = n === a[i + 1];
!prev && next && acc.push([]);
(prev || next ? acc[acc.length - 1] : acc).push(n);
return acc;
}, []);