문제
내가 작성한 정답
map
import java.util.*;
class Solution {
public int solution(String[] strArr) {
Map<Integer,Integer> map = new HashMap<>();
for(String s : strArr){
map.put(s.length(),map.getOrDefault(s.length(),0)+1);
}
Collection<Integer> counts = map.values();
int answer=0;
for(int i : counts){
if(answer<i) answer = i;
}
return answer;
}
}
다른 사람들의 정답
Math.max
class Solution {
public int solution(String[] strArr) {
int answer = 0;
int[] lengArr = new int[31];
for(int i=0; i<strArr.length; i++) {
lengArr[strArr[i].length()]++;
}
for(int i=0; i<=30; i++) {
answer = Math.max(answer, lengArr[i]);
}
return answer;
}
}
Collectors.groupingBy(String::length)
import java.util.*;
import java.util.stream.Collectors;
class Solution {
public int solution(String[] strArr) {
return Arrays.stream(strArr)
.collect(Collectors.groupingBy(String::length))
// Map<Integer,List<>> : key-length, value-List<String>
// 기준이 되는 것이 key - length
.values()
.stream()
.max(Comparator.comparingInt(List::size))
.orElse(Collections.emptyList())
.size();
}
}

Share article