Skip to content

Instantly share code, notes, and snippets.

@sebs
Created October 2, 2014 14:09
Show Gist options
  • Select an option

  • Save sebs/fab970e84799adbbd83e to your computer and use it in GitHub Desktop.

Select an option

Save sebs/fab970e84799adbbd83e to your computer and use it in GitHub Desktop.
// you can use console.log for debugging purposes, i.e.
// console.log('this is a debug message');
function solution(X, A) {
// write your code in JavaScript (ECMA-262, 5th edition)
var isNotEqual = 0;
A.forEach(function(item) {
if (item !== X) {
isNotEqual++;
}
});
// to understand the problem a little better
var bucket1 = isNotEqual;
var bucket2 = 0;
var returnValue = -1;
// make equal buckets
A.some(function(item, index) {
if (item === X) {
bucket2++
} else {
bucket1--;
}
if (bucket1 === bucket2 && (bucket1 !== 0 && bucket2 !== 0)) {
returnValue = index + 1;
return true;
}
});
return returnValue;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment