Created
April 16, 2015 20:49
-
-
Save sdfsdhgjkbmnmxc/cf637d89e8df4b0a1b9d to your computer and use it in GitHub Desktop.
This file contains 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
1 using System; | |
2 class Solution { | |
3 public int solution(int[] A) { | |
4 int n = A.Length; | |
5 int[] L = new int[n + 1]; | |
6 L[0] = -1; | |
7 for (int i = 0; i < n; i++) { | |
8 L[i + 1] = A[i]; | |
9 } | |
10 int count = 0; | |
11 int pos = (n + 1) / 2; | |
12 int candidate = L[pos]; | |
13 for (int i = 1; i <= n; i++) { | |
14 if (L[i] == candidate) | |
15 count = count + 1; | |
16 } | |
17 if (count > pos) | |
18 return candidate; | |
19 return (-1); | |
20 } | |
21 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment