Skip to content

Instantly share code, notes, and snippets.

@gauravbansal74
Last active January 13, 2018 07:16
Show Gist options
  • Save gauravbansal74/c09e9735cce784a8631fd7dd3fafe1c5 to your computer and use it in GitHub Desktop.
Save gauravbansal74/c09e9735cce784a8631fd7dd3fafe1c5 to your computer and use it in GitHub Desktop.
Codility:BugfixingLeaderSorted
function solution(A) {
var n = A.length;
var L = new Array(n + 1);
L[0] = -1;
var i;
for (i = 0; i < n; i++) {
L[i + 1] = A[i];
}
var count = 0;
var pos = Math.floor((n/2)+1);
candidate = L[pos];
for (i = 1; i <= n; i++) {
if (L[i] == candidate)
count = count + 1;
}
if (count >= pos)
return candidate;
return (-1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment