[ 문제 ]
알파벳 대소문자로 된 단어가 주어지면, 이 단어에서 가장 많이 사용된 알파벳이 무엇인지 알아내는 프로그램을 작성하시오. 단, 대문자와 소문자를 구분하지 않는다.
package print_h;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class bj1157 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] str = br.readLine().toUpperCase().split("");
// 배열을 정렬
Arrays.sort(str);
int max = 0;
int tmpw = 0;
String m = ""; //최대 개수를 갖는 문자
String tmp = ""; // 임시로 문자를 저정할 문자열 변수
for(int i = 0 ; i < str.length ; i++) {
if(!tmp.equals(str[i])) {
tmp = str[i];
tmpw = 0;
}
tmpw += 1;
if(tmpw > max) {
max = tmpw;
m = str[i];
} else if (tmpw == max && !m.equals(tmp)) {
//동일한 값이 있는 경우 결과를 "?"로 바꿔둠.
m = "?";
}
}
System.out.println(m);
}
}