Last active
January 13, 2018 07:16
-
-
Save gauravbansal74/c09e9735cce784a8631fd7dd3fafe1c5 to your computer and use it in GitHub Desktop.
Codility:BugfixingLeaderSorted
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
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