//统计数组中出现次数超过一半的数字 #include <stdio.h> int Find(int *arr, int len) { int num = 0; //当前数字 int times = 0; //当前数字出现的次数 int i = 0; for (i = 0; i<len; i++) { if (times == 0) { num = arr[i]; times = 1; } else if (arr[i] == num) times++; else times--; }
1.背景 想要统计这一个字符串数组中每一个非重复字符串的数量,使用map来保存其key和value.这个需求在实际开发中经常使用到,我以前总是新建一个空数组来记录不重复字符串,并使用计数器计数,效率低下且麻烦,特此记录. 2.代码实现 public class test { public void makeEqual(String[] words) { Map<String,Integer> maps = new HashMap<>(); for (String str : wor