From c82a5324e22d5a9ed59e4d328bea5f6d70d741c3 Mon Sep 17 00:00:00 2001 From: mordiumaco Date: Thu, 16 Jul 2020 01:16:32 +0900 Subject: [PATCH] =?UTF-8?q?1-8=20=EC=99=84=EC=A3=BC=ED=95=98=EC=A7=80?= =?UTF-8?q?=EB=AA=BB=ED=95=9C=20=EC=84=A0=EC=88=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Solution.java" | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git "a/week2/1. \354\231\204\354\243\274\355\225\230\354\247\200 \353\252\273\355\225\234 \354\204\240\354\210\230/Solution.java" "b/week2/1. \354\231\204\354\243\274\355\225\230\354\247\200 \353\252\273\355\225\234 \354\204\240\354\210\230/Solution.java" index 23d7824..172f2b2 100644 --- "a/week2/1. \354\231\204\354\243\274\355\225\230\354\247\200 \353\252\273\355\225\234 \354\204\240\354\210\230/Solution.java" +++ "b/week2/1. \354\231\204\354\243\274\355\225\230\354\247\200 \353\252\273\355\225\234 \354\204\240\354\210\230/Solution.java" @@ -1,7 +1,60 @@ +import java.util.Arrays; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Map.Entry; + /* 1. 완주하지 못한 선수 https://programmers.co.kr/learn/courses/30/lessons/42576 */ public class Solution { + public static void main(String[] args) { + + String[] participant = { "leo", "kiki", "eden" }; + String[] completion = { "eden", "kiki" }; + + System.out.println(solution(participant, completion)); + System.out.println(solution2(participant, completion)); + } + + public static String solution(String[] participant, String[] completion) { + + Arrays.sort(participant); + Arrays.sort(completion); + + for (int i = 0; i < completion.length; i++) { + if (!participant[i].equals(completion[i])) { + return participant[i]; + } + } + + return participant[participant.length - 1]; + } + + public static String solution2(String[] participant, String[] completion) { + + Map checkerMap = new HashMap<>(); + + for (String person : participant) { + checkerMap.put(person, checkerMap.getOrDefault(person, 0) + 1); + } + + for (String person : completion) { + checkerMap.put(person, checkerMap.getOrDefault(person, 0) - 1); + } + + Iterator> it = checkerMap.entrySet().iterator(); + + while (it.hasNext()) { + + Entry mapEntry = it.next(); + + if (mapEntry.getValue() == 1) { + return mapEntry.getKey(); + } + } + return null; + } } \ No newline at end of file