查找法
public class FindTheMostAppearChar {
public static void main(String
[] args
) {
hashMapMethodToAchieve();
}
public static void hashMapMethodToAchieve() {
Scanner scanner
= new Scanner(System
.in
);
String string
= scanner
.nextLine().trim();
scanner
.close();
char[] arr
= string
.toCharArray();
Map
<Character, Integer> map
= new HashMap<>();
if (arr
!= null
&& arr
.length
> 0) {
for (int i
= 0; i
< arr
.length
; i
++) {
if (map
.get(arr
[i
]) != null
) {
map
.put(arr
[i
],map
.get(arr
[i
]) + 1);
} else {
map
.put(arr
[i
], 1);
}
}
}
FindTheMostCharByMap(map
);
}
public statice
void FindTheMostCharByMap(Map
<Character, Integer> map
) {
Set
<Character> keys
= map
.keySet();
Iterator iterator
= keys
.iterator();
Character maxKey
= (Character
) iterator
.next();
int maxValue
= map
.get(maxKey
);
while (iterator
.hasNext()) {
Character temp
= (Character
) iterator
.next();
if (maxValue
< map
.get(temp
)) {
maxKey
= temp
;
maxValue
= map
.get(temp
);
}
}
System
.out
.println("出现次数最多的字符是:" + maxKey
+ ", 出现的次数:" + maxValue
);
}
public static void FindTheMostCharByMapEntry(Map
<Character, Integer> map
) {
Iterator iterator
= map
.entrySet().iterator();
Map
.Entry entry
= (Map
.Entry
) iterator
.next();
char maxKey
= (char) entry
.getKey();
int maxValue
= (int) entry
.getValue();
while (iterator
.hasNext()) {
entry
= (Map
.Entry
) iterator
.next();
char tempKey
= (char) entry
.getKey();
int t
删除法
class Solution {
Map
<String,Integer> deleteMethodToAchieve(){
System
.out
.println("请输入一段字符串");
Scanner s1
=new Scanner(System
.in
);
String s2
=s1
.nextLine().trim();
int max
=0;
String strmax
="";
while (s2
.length()>0){
int prelength
=s2
.length();
String firstchar
=s2
.substring(0,1);
s2
=s2
.replaceAll(firstchar
,"");
int curlength
=s2
.length();
if(prelength
-curlength
>max
){
max
=prelength
-curlength
;
strmax
=firstchar
;
}
}
Map
<String,Integer> res
= new HashMap<>();
res
.put(strmax
,max
);
return res
;
}
}
排序法(2种实现)
class Solution {
Map
<Character,Integer> sortMethodToAchieve() {
Scanner s1
=new Scanner(System
.in
);
String str_input
=s1
.nextLine();
char[] s2
=str_input
.toCharArray();
Arrays
.sort(s2
);
char temp_char
=s2
[0];
int temp_cishu
=0;
char max_char
=s2
[0];
int max_cishu
=0;
for(char i
: s2
){
if (temp_char
==i
){
temp_cishu
+=1;
}
else {
temp_char
=i
;
temp_cishu
=1;
}
if(temp_cishu
>max_cishu
){
max_cishu
=temp_cishu
;
max_char
=temp_char
;
}
}
Map
<Character,Integer> res
= new HashMap<>();
res
.put(max_char
,max_cishu
);
return res
;
}
#方法
2
public static void sortMethodToAchieve2() {
Scanner scanner
= new Scanner(System
.in
);
String string
= scanner
.nextLine().trim();
scanner
.close();
char[] arr
= string
.toCharArray();
Arrays
.sort(arr
);
char maxValue
= 'a';
int maxCount
= 1;
int count
= 1;
for (int i
= 0; i
< arr
.length
- 1; i
++) {
if (arr
[i
] == arr
[i
+1]) {
count
++;
}
if (arr
[i
] != arr
[i
+1]) {
count
= 1;
}
if (count
> maxCount
) {
maxCount
= count
;
maxValue
= arr
[i
];
}
}
System
.out
.println("出现次数最多的字符是:" + maxValue
+ ", 出现的次数:" + maxCount
);
}
}
参考链接 https://segmentfault.com/a/1190000019536395?utm_source=tag-newest
转载请注明原文地址:https://blackberry.8miu.com/read-27434.html