Skip to content

Instantly share code, notes, and snippets.

@BeKnowDo
Created June 12, 2017 18:16
Show Gist options
  • Save BeKnowDo/8dd506ea2a6e3f946807558aebadee69 to your computer and use it in GitHub Desktop.
Save BeKnowDo/8dd506ea2a6e3f946807558aebadee69 to your computer and use it in GitHub Desktop.
Contiguous Groups
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