Skip to content

Instantly share code, notes, and snippets.

@robabby
Created November 8, 2015 21:59
Show Gist options
  • Select an option

  • Save robabby/c248d6151cab405ce83e to your computer and use it in GitHub Desktop.

Select an option

Save robabby/c248d6151cab405ce83e to your computer and use it in GitHub Desktop.
function occurences(n, arr) {
var first = -1;
var last = -1;
if (arr === null || arr.length === 0 || arr[0] > n || arr[arr.length - 1] < n) {
return [first, last];
}
for (var i = 0; i < arr.length; i++) {
var num = arr[i];
if (num === n) {
if (first < 0) {
first = i;
} else {
last = i;
}
} else if (num > n) {
return [first, last];
}
}
return [first, last];
}
console.log(occurences(7, [1, 2, 3, 6, 7, 7, 8]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment