Created
June 12, 2017 18:16
-
-
Save BeKnowDo/8dd506ea2a6e3f946807558aebadee69 to your computer and use it in GitHub Desktop.
Contiguous Groups
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ContiguousGroups = (array) => { | |
const arrayValues = array.sort(); | |
let contiguousCount = 0, | |
foundMatch = false; | |
arrayValues.map(function(item, index) { | |
let findDifference = Math.abs(item - arrayValues[index + 1]); | |
if (!isNaN(findDifference) && findDifference === 0) { | |
contiguousCount++; | |
foundMatch = true; | |
} | |
if (foundMatch === true && !isNaN(findDifference) && arrayValues[index + 1] !== 'undefined' && findDifference === 1) { | |
contiguousCount++; | |
} | |
}); | |
return contiguousCount; | |
} | |
ContiguousGroups([5, 5, 1, 2, 6, 8, 9]); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment