Check out Codility training tasks
Tasks Details
easy
Find an index of an array such that its value occurs at more than half of indices in the array.
Task Score
100%
Correctness
100%
Performance
100%

对于一个给定的整数数组, "支配者"是在这个数组中出现的频率超过一半的整数.

例如:

A[0] = 3 A[1] = 4 A[2] = 3 A[3] = 2 A[4] = 3 A[5] = -1 A[6] = 3 A[7] = 3

数值"3"出现过5次, 5/8 > 0.5, 所以数值"3"是一个"支配者";

而在这个数组中, 这个"支配者"出现在数组下标:

  • 0, 2, 4, 6 , 7.

请写一个函数

def solution(A)

对给定数组返回其任意一个支配者的数组下标。

例如,对上述数组,函数可以返回0,2,4,6,7中的任意一个。 如果没有支配者,函数应该返回 −1。

假定:

  • N 是 [0..100,000] 内的 整数;
  • 数组 A 每个元素是取值范围 [−2,147,483,648..2,147,483,647] 内的 整数 .
Copyright 2009–2025 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used Python
Time spent on task 4 minutes
Notes
not defined yet