Created
October 2, 2014 14:09
-
-
Save sebs/fab970e84799adbbd83e to your computer and use it in GitHub Desktop.
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
| // 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