Copy the code code as follows:
/**
* Name: Find the number and the number of repetitions of the elements in the array that are the most
* Description:
* The elements in the array may be repeated. This method can find the number with the most repetitions and return how many times it has been repeated.
* But you need to know what the largest element in this array is. If you can't determine it, it will be a tragedy~
*
* @param array target array;
* The maximum value of the data in the max array;
* @return Returns a map collection containing the number with the most repetitions (value) and the number of repetitions (maxCount);
* An internal exception occurs and returns 0 by default;
* @throws
* @Author Yang Yuan
*/
public static Map<String, Integer> arraySearch(int[] array,int max){
//result collection
Map<String, Integer> resultMap = new HashMap<String, Integer>();
//Number of repetitions
int maxCount = 0;
//Number with the most number of repetitions
int value = 0;
try{
//Initialize the data array to store the number of occurrences of each element
int[] dataArray = new int[max+1];
//Traverse the array to be found, use each element as a subscript, directly locate the data array, and perform a +1 operation to indicate that it appears once
for(int i : array){
dataArray[i]++;
}
//Find the maximum value in the data array
for(int i=0;i<dataArray.length;i++){
if(dataArray[i]>maxCount){
maxCount=dataArray[i];
value=i;
}
}
}catch (Exception e) {}
resultMap.put("maxCount", maxCount);
resultMap.put("value", value);
return resultMap;
}
/**
* Name: Compare the sizes of two strings
* Description: The comparison rules are consistent with the order by effect in the database;
* Null is automatically converted to empty, and the empty string is the largest;
*
* @param first The first string to be compared;
* second The second string to be compared;
* @return first is greater than second and returns a positive number;
* first is equal to second and returns 0;
* first is less than second and returns a negative number;
* Internal exception returns 0 by default;
* The return value is not a fixed value~~;
* @throws
* @Author Yang Yuan
*/
public static int compareString(String first,String second){
int result = 0;
try{
//null to empty
first = first==null?"":first;
second = second==null?"":second;
//Pre-record the string length to avoid repeated reading
int firstLength=first.length();
int secondLength=second.length();
//Handle special cases containing empty strings
if("".equals(first) || "".equals(second)){
//Who is taller and who is smaller?
result = secondLength-firstLength;
}else{
//Temporary space used to store the sum of ascii codes
int firstCount = 0;
int secondCount = 0;
// Use pure operations to find the smaller of the two numbers, which is actually bt
int minLength = (secondLength*(firstLength/secondLength) + firstLength*(secondLength/firstLength))/(firstLength/secondLength + secondLength/firstLength);
//Intercept bit by bit according to the shorter number of digits in the two strings to prevent out of bounds
for(int i=0;i<minLength;i++){
//Find the ascii code sum
firstCount+=first.substring(i,i+1).getBytes()[0];
secondCount+=second.substring(i,i+1).getBytes()[0];
//The sum is not equal, indicating that the size has been compared
if(firstCount!=secondCount){
break;
}
}
if(firstCount==secondCount){
//The length is too long
result = firstLength-secondLength;
}else{
//The sum is as big as possible
result = firstCount-secondCount;
}
}
}catch (Exception e) {}
return result;
}