Learn this from stackflow.

public class test {

public static void main(String[] args) throws IOException

{

int [] a={1,2,3,4,4,4,5,4,4};

int r=GetMajorElement(a);

System.out.println(r);

}

//check the element in the array occurs more than half of the array length

public static int GetMajorElement(int[] a)

{

int r=a[0];

int count=1;

for(int i=1;i<a.length;i++)

{

if(r==a[i])

count++;

else count--;

if(count==0)

{        r=a[i]; count++;}

}

return r>=a.length/2?r:-1;

}

}

check the element in the array occurs more than half of the array length的更多相关文章

  1. Array.new(5, [1, 2, 3]) or Array.new(5) { [1, 2, 3] }的差别

    Array.new(5, [1, 2, 3]) or Array.new(5) { [1, 2, 3] } Array.new(size, default_object) creates an arr ...

  2. ExtJS学习-----------Ext.Array,ExtJS对javascript中的Array的扩展

    关于ExtJS对javascript中的Array的扩展.能够參考其帮助文档,文档下载地址:http://download.csdn.net/detail/z1137730824/7748893 因为 ...

  3. [array] leetcode - 33. Search in Rotated Sorted Array - Medium

    leetcode - 33. Search in Rotated Sorted Array - Medium descrition Suppose an array sorted in ascendi ...

  4. Why does typeof array with objects return “Object” and not “Array”?

    https://stackoverflow.com/questions/4775722/check-if-object-is-an-array One of the weird behaviour a ...

  5. LeetCode Array Easy 167. Two Sum II - Input array is sorted

    Description Given an array of integers that is already sorted in ascending order, find two numbers s ...

  6. LeetCode Array Easy 26.Remove Duplicates from Sorted Array 解答及疑惑

    Description Given a sorted array nums, remove the duplicates in-place such that each element appear ...

  7. leetcode 153. Find Minimum in Rotated Sorted Array 、154. Find Minimum in Rotated Sorted Array II 、33. Search in Rotated Sorted Array 、81. Search in Rotated Sorted Array II 、704. Binary Search

    这4个题都是针对旋转的排序数组.其中153.154是在旋转的排序数组中找最小值,33.81是在旋转的排序数组中找一个固定的值.且153和33都是没有重复数值的数组,154.81都是针对各自问题的版本1 ...

  8. Array.prototype.push.apply(a,b)和Array.prototype.slice.call(arguments)

    Array.prototype.push.apply(a,b) 时常看到在操作数组的时候有这样的写法: var a = [1,2,3]; var b = [4,5,6]; a.push.apply(a ...

  9. ExtJS学习-----------Ext.Array,ExtJS对javascript中的Array的扩展(实例)

    (1)clean var arr = [1,2,null,3,'']; alert(Ext.Array.clean(arr)); //clean的对象:(value === null) || (val ...

随机推荐

  1. 小巧方便的MVC后端验证码,供大家学习借鉴

    调用: public ActionResult Vcode()//验证码 { string code = ValidateCode.CreateRandomCode(4); ValidateCode. ...

  2. EF工作中踩过的坑.

    1.EF同一个linq里边不支持两个或两个以上不同dbcontext的使用,必须拆解开才能使用; ef也不支持自定义集合和dbcontext属性的混合使用. 2.如果要用用统一域账号连接databas ...

  3. Ajax,谷歌提示AutoCompleteExtender控件

    提示内容从数据库中读取: ------------------------------------------页面 <asp:ScriptManager ID="ScriptManag ...

  4. macbook 我们需要买吗

    能否写出好代码与是否使用“好”的电脑是没有直接关系的.

  5. css导航栏

    几个导航栏也算对学过知识的回顾,总有新的收获,下面是学习过程中敲的代码: <!DOCTYPE HTML> <html> <head> <title>&l ...

  6. thinkPHP学习笔记(2)

    1.调试模式 设置调试模式部分代码如下: <?php define('APP_DEBUG',TRUE); // 开启调试模式 常量定义代码 require '/ThinkPHP框架所在目录/Th ...

  7. 解决SSIS中的脚本任务无法调试的问题

    开发环境:操作系统环境为Win7 64Bit,数据库为SQLServer2008R2 问题现象:在SSIS的项目工程中,新建Package包,新建脚本任务,编写脚本程序以后,设置断点无法调试(Debu ...

  8. iOS— UIScrollView和 UIPageControl之间的那些事

    本代码主要实现在固定的位置滑动图片可以切换. 目录图如下: ViewController.h #import <UIKit/UIKit.h> // 通过宏定义定义宽和高 #define W ...

  9. IOS开发中NSRunloop跟NSTimer的问题

    在Windows时代,大家肯定对SendMessage,PostMessage,GetMessage有所了解,这些都是windows中的消息处理函数,那对应在ios中是什么呢,其实就是NSRunloo ...

  10. object-c中的类目,延展,协议

    协议 协议只有方法的声明(类似于其他编程语言的接口)   协议相当于大家都所遵循的 关键字 @protocol 协议名 <所遵循的协议> 默认NSObject   @end     @pr ...