2021. 4. 17. 17:09
프로그래머스에 해쉬 문제 풀다가 조합공식에 넣을 중복되는 원소 갯수 체크하는 방법이 필요했음.
샘플 데이터의 카테고리 (headgear, eyewear) 별로 원소 갯수.
String str[][] =
{
{"yellowhat", "headgear"},
{"bluesunglasses", "eyewear"},
{"green_turban", "headgear"}
};
HashMap<String, Integer> hashMap = new HashMap<String, Integer>();
for(String[] str : clothes) {
hashMap.put(str[1], hashMap.getOrDefault(str[1], 0) + 1 );
}
System.out.println(hashMap);
for(Entry<String, Integer> str : hashMap.entrySet())
{
System.out.println("value : " + str.getValue());
}
'AREA(지속적인 일상) > 01_Programmers' 카테고리의 다른 글
[Programmers] 코딩테스트 연습 - 두 개 뽑아서 더하기 (0) | 2020.10.13 |
---|