首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Java找到数组中唯一的出现奇数次的数

2012-08-21 
Java找出数组中唯一的出现奇数次的数import java.util.*public class FindOdd {public static void main(

Java找出数组中唯一的出现奇数次的数

import java.util.*;public class FindOdd {public static void main(String[] args) {int [] arr = {1, 4, 7, 3, 1, 3, 7, 4, 6, 9, 6};Map<Integer, Integer> map = new HashMap<Integer, Integer>();for(int i=0; i<arr.length; i++) {if(map.containsKey(arr[i])) {map.remove(arr[i]);//出现了偶数次则删掉} else {map.put(arr[i], 1);//还未出现过则次数设为1}}Iterator iterator = map.keySet().iterator();while(iterator.hasNext()) {System.out.println(iterator.next());}}}

热点排行