Skip to content

Instantly share code, notes, and snippets.

@nedgrady
Last active August 6, 2021 21:03
Show Gist options
  • Save nedgrady/6bba7584369ea267aa248669ee46d2a9 to your computer and use it in GitHub Desktop.
Save nedgrady/6bba7584369ea267aa248669ee46d2a9 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++
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