Skip to content

Instantly share code, notes, and snippets.

@ChrisDobby
Created February 28, 2023 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChrisDobby/af6462abf3d9181ee5e98c189cf20534 to your computer and use it in GitHub Desktop.
Save ChrisDobby/af6462abf3d9181ee5e98c189cf20534 to your computer and use it in GitHub Desktop.
Given a list of numbers, return all groups of repeating consecutive numbers
const repeatedGroups = (numbers: number[]) =>
numbers
.reduce<number[][]>(
(acc, num) => (acc.length === 0 || !acc[acc.length - 1].includes(num) ? [...acc, [num]] : [...acc.slice(0, acc.length - 1), [...acc[acc.length - 1], num]]),
[]
)
.filter(nums => nums.length > 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment