查找数组中的元素多数 [英] Find the majority element in array

查看:178
本文介绍了查找数组中的元素多数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的大部分元素是发生超过阵列的大小的一半的元素

The majority element is the element that occurs more than half of the size of the array.

如何找到大部分元素为O阵列(N)?

How to find the majority element in an array in O(n)?

推荐答案

大多数元素(如果存在)也将是中位数。我们可以发现在O(n)的中位数,然后检查它确实是在O有效的大部分元素(N)。

The majority element (if it exists) will also be the median. We can find the median in O(n) and then check that it is indeed a valid majority element in O(n).

这篇关于查找数组中的元素多数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆