Skip to content

Instantly share code, notes, and snippets.

@nedgrady
Created August 6, 2021 22:25
Show Gist options
  • Save nedgrady/d81c876d5a44888deda851ad2a78ab8e to your computer and use it in GitHub Desktop.
Save nedgrady/d81c876d5a44888deda851ad2a78ab8e to your computer and use it in GitHub Desktop.
function areSomeNumberOfConsecutiveItemsPresentInArray(
targetNumberOfConsecutiveItems: number,
array: unknown[]
) {
let numberOfConsecutiveFound = 1
let previousItem: unknown = {}
for (const currentItem of array) {
if (currentItem && currentItem === previousItem)
numberOfConsecutiveFound++
else
numberOfConsecutiveFound = 1 // reset the total number of consecutive items count!
// Until we’ve found a winner
if (numberOfConsecutiveFound === targetNumberOfConsecutiveItems)
return currentItem
previousItem = currentItem
}
return null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment