169. Majority Element.C#

Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.

You may assume that the array is non-empty and the majority element always exist in the array.

Example 1:

Input: [3,2,3]
Output: 3
Example 2:

Input: [2,2,1,1,1,2,2]
Output: 2

public class Solution {
    public int MajorityElement(int[] nums) {
        //排序取中间元素
        Array.Sort(nums);
        return nums[nums.Length/2];
    }
}  
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 13,140评论 0 13
  • 我们从一出生就开始安装程序。各种学习都是程序的安装。父母的各种教育,老师的各种教导,都是一个个隐性程序的安装。 长...
    雪飞_dd6c阅读 2,394评论 0 1
  • 当你选择将孩子生下来,你会成一个父亲或者母亲,人生因此而改变; 选择决定命运,这是战略问题,与眼光有关,更受理念影...
    绘本推荐狮阅读 2,490评论 0 3
  • 母亲一个人 守着一座老宅 还有一棵老树,和树上的鸟儿 她一声接一声地咳嗽 为了打破那空旷的沉寂 有时,她佝着背 去...
    空谷幽兰_9d14阅读 4,751评论 26 30
  • 在路上的时光不知道要怎样形容,时而开心快乐,时而矛盾傲脑,不知道什么时候就想放弃,听着别人的脚步声,亦踱亦缓,感叹...
    凯歌衔采薇1998阅读 1,365评论 0 1