复制代码 代码如下:
public static Map
//结果集合
Map
//重复的次数
int maxCount = 0;
//重复次数对多的数
int value = 0;
try{
//初始化数据数组,用来存放每个元素出现的次数
int[] dataArray = new int[max+1];
//遍历要查找的数组,以每个元素为下标,直接定位数据数组,进行+1操作,表示出现了一次
for(int i : array){
dataArray[i]++;
}
//找到数据数组中最大值
for(int i=0;i
maxCount=dataArray[i];
value=i;
}
}
}catch (Exception e) {}
resultMap.put("maxCount", maxCount);
resultMap.put("value", value);
return resultMap;
}
public static int compareString(String first,String second){
int result = 0;
try{
//null转空
first = first==null?"":first;
second = second==null?"":second;
//预先记录字符串长度,避免反复读取
int firstLength=first.length();
int secondLength=second.length();
//处理含有空串的特殊情况
if("".equals(first) || "".equals(second)){
//谁长谁小
result = secondLength-firstLength;
}else{
//临时空间,用来存放ascii码总和
int firstCount = 0;
int secondCount = 0;
//用纯运算得出两个数中较小的数,实在是bt
int minLength = (secondLength*(firstLength/secondLength) + firstLength*(secondLength/firstLength))/(firstLength/secondLength + secondLength/firstLength);
//按两个字符串中较短的位数去逐位截取,防止越界
for(int i=0;i
firstCount+=first.substring(i,i+1).getBytes()[0];
secondCount+=second.substring(i,i+1).getBytes()[0];
//和不相等,说明已经比较出了大小
if(firstCount!=secondCount){
break;
}
}
if(firstCount==secondCount){
//长度长的大
result = firstLength-secondLength;
}else{
//总和大的大
result = firstCount-secondCount;
}
}
}catch (Exception e) {}
return result;
}



