check the element in the array occurs more than half of the array length
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的更多相关文章
- 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 ...
- ExtJS学习-----------Ext.Array,ExtJS对javascript中的Array的扩展
关于ExtJS对javascript中的Array的扩展.能够參考其帮助文档,文档下载地址:http://download.csdn.net/detail/z1137730824/7748893 因为 ...
- [array] leetcode - 33. Search in Rotated Sorted Array - Medium
leetcode - 33. Search in Rotated Sorted Array - Medium descrition Suppose an array sorted in ascendi ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- ExtJS学习-----------Ext.Array,ExtJS对javascript中的Array的扩展(实例)
(1)clean var arr = [1,2,null,3,'']; alert(Ext.Array.clean(arr)); //clean的对象:(value === null) || (val ...
随机推荐
- asp.net.mvc 的单文件上传和多文件上传的简单例子
首先打开vs2012,创建空的mvc4项目,名称为MVCStudy,选择基本模板
- Java-链表LinkedList源码原理分析,并且通过LinkedList构建队列
在这里我们介绍一下最简单的链表LinkedList: 看一下add()方法: public boolean add(E e) { linkLast(e); return true; } void li ...
- Python数学运算的一个小算法(求一元二次方程的实根)
请定义一个函数quadratic(a, b, c),接收3个参数,返回一元二次方程:ax² + bx + c = 0的两个解. #!/usr/bin/env python # -*- coding: ...
- H5调用Android拨打电话
1.AndroidAndJSInterface.java class AndroidAndJSInterface { /** * 该方法将被js调用,用于加载数据 */ @JavascriptInte ...
- OS X 在Cisco无线环境下丢包分析 part 2
part 1说到,单播的ARP请求最终都被网关丢弃了,从而造成了丢包.先说我最终怎么解决的吧,我最终把核心交换上针对无线VLAN的arp inspection和dhcp snooping删掉了,然后出 ...
- JSDoc那些事
几天工作上需要文档化一些Javascript东西,所以在找一些JS文档化工具,以下分析几种工具. 1.JSDoc-toolkit 一开始还想用这个工具,但后来在解析生成文档时候,出现了很严重的错误,还 ...
- 初学Node(五)文件I/O
文件读写 Node的出现的一个亮点就是让JS也有了读写文件的能力,而且实现起来要比其他语言更简单,对文件的一些操作我们都可通过fs模块来完成.fs即fileSystem的缩写,fs模块可以完成对文件的 ...
- Microsoft Dynamics CRM 2013 安装过程 图解
在安装前,先持一下SQL配置管理,将相关的服务打开.(由于在虚拟机里,许多服务需要时才会打开,像Reporting Services需要处理报表时才打开) 注:Analysis Services 登录 ...
- Atitit.为什么小公司也要做高大上开源项目
Atitit.为什么小公司也要做高大上开源项目 1. 为什么手头有很多加急的事情还要做高大上开源项目??1 2. 从长远看,发展 高大上开源项目计划对于解决我们在应急项目正面临着的种种严峻问题也大有裨 ...
- 【原】自定义UIPageControl的圆点
在下面的两种情况下会导致圆点贴图刷新: 1.用户调用setCurrentPage:(NSInteger)currentPage时 所以重载这个函数便可拦截 2.点击圆点矩形区域时 这说明,我们 ...