|
2 | 2 | //완벽한 정답이 아닙니다.
|
3 | 3 | //정답 1 - codeisneverodd
|
4 | 4 | function solution(participant, completion) {
|
5 | | -var answer = ""; |
6 | | -participant = participant.sort(); |
7 | | -completion = completion.sort(); |
8 | | -for (let i = 0, len = completion.length; i < len; i++) { |
9 | | -if (participant[i] !== completion[i]) return participant[i]; |
10 | | -} |
11 | | -answer = participant[participant.length - 1]; |
12 | | -return answer; |
| 5 | +var answer = ""; |
| 6 | +participant = participant.sort(); |
| 7 | +completion = completion.sort(); |
| 8 | +for (let i = 0, len = completion.length; i < len; i++) { |
| 9 | +if (participant[i] !== completion[i]) return participant[i]; |
| 10 | +} |
| 11 | +answer = participant[participant.length - 1]; |
| 12 | +return answer; |
13 | 13 | }
|
14 | 14 |
|
15 | 15 | //정답 2 - jaewon1676
|
16 | 16 | function solution(participant, completion) {
|
17 | | -var answer = ""; |
18 | | -for (let i = 0; i < participant.length; i++) { |
19 | | -for (let j = 0; j < completion.length; j++) { |
20 | | -if (participant[i] === completion[j]) { |
21 | | -console.log(participant, completion); |
22 | | -participant.splice(i, 1); |
23 | | -completion.splice(j, 1); |
24 | | -i--; |
25 | | -j--; |
26 | | -console.log(participant, completion); |
27 | | -break; |
28 | | -} |
29 | | -} |
30 | | -} |
| 17 | +var answer = ""; |
| 18 | +for (let i = 0; i < participant.length; i++) { |
| 19 | +for (let j = 0; j < completion.length; j++) { |
| 20 | +if (participant[i] === completion[j]) { |
| 21 | +console.log(participant, completion); |
| 22 | +participant.splice(i, 1); |
| 23 | +completion.splice(j, 1); |
| 24 | +i--; |
| 25 | +j--; |
| 26 | +console.log(participant, completion); |
| 27 | +break; |
| 28 | +} |
| 29 | +} |
| 30 | +} |
31 | 31 |
|
32 | | -return participant[0]; |
| 32 | +return participant[0]; |
33 | 33 | }
|
34 | 34 |
|
35 | 35 | //완벽한 정답이 아닙니다.
|
36 | 36 | //정답 3 - hyosung
|
37 | 37 | function solution(participant, completion) {
|
38 | | - let answer = ""; |
39 | | - // 2개 이상을 가진 특정값의 갯수 기록용 변수 |
40 | | - let max = 0; |
41 | | - // 반복문 내부에서 set.has 를 사용하기 위해 Set 선언 (처음에는 Array.findIndex 를 사용) |
42 | | - const set = new Set([...completion]); |
43 | | - // 반복문 최적화 - 반복되던 연산 제거 (값 비교, length) |
44 | | - const length = participant.length; |
45 | | - for (let i = length; i--; ) { |
46 | | - // 완주자 명단에 없다면 완주하지 못한 참가자 이므로 바로 종료 |
47 | | - if (!set.has(participant[i])) { |
48 | | - answer = participant[i]; |
49 | | - break; |
50 | | - } |
51 | | - // 배열안에 특정값 갯수 확인 |
52 | | - let count = participant.reduce( |
53 | | - (a, v) => (v === participant[i] ? a + 1 : a), |
54 | | - 0 |
55 | | - ); |
56 | | - // 해당 값이 참가자 그룹 내 2명 이상이고 이전 최대 동명이인 참가자보다 많다면 |
57 | | - // 해당 로직을 반복하면 제일 많은 동명이인을 알 수 있다 |
58 | | - if (count > 1 && max < count) { |
59 | | - answer = participant[i]; |
60 | | - // 조건에 맞는 동명이인 수 저장 |
61 | | - max = count; |
62 | | - } |
63 | | - } |
64 | | - return answer; |
| 38 | + let answer = ""; |
| 39 | + // 2개 이상을 가진 특정값의 갯수 기록용 변수 |
| 40 | + let max = 0; |
| 41 | + // 반복문 내부에서 set.has 를 사용하기 위해 Set 선언 (처음에는 Array.findIndex 를 사용) |
| 42 | + const set = new Set([...completion]); |
| 43 | + // 반복문 최적화 - 반복되던 연산 제거 (값 비교, length) |
| 44 | + const length = participant.length; |
| 45 | + for (let i = length; i--; ) { |
| 46 | + // 완주자 명단에 없다면 완주하지 못한 참가자 이므로 바로 종료 |
| 47 | + if (!set.has(participant[i])) { |
| 48 | + answer = participant[i]; |
| 49 | + break; |
| 50 | + } |
| 51 | + // 배열안에 특정값 갯수 확인 |
| 52 | + let count = participant.reduce( |
| 53 | + (a, v) => (v === participant[i] ? a + 1 : a), |
| 54 | + 0 |
| 55 | + ); |
| 56 | + // 해당 값이 참가자 그룹 내 2명 이상이고 이전 최대 동명이인 참가자보다 많다면 |
| 57 | + // 해당 로직을 반복하면 제일 많은 동명이인을 알 수 있다 |
| 58 | + if (count > 1 && max < count) { |
| 59 | + answer = participant[i]; |
| 60 | + // 조건에 맞는 동명이인 수 저장 |
| 61 | + max = count; |
| 62 | + } |
| 63 | + } |
| 64 | + return answer; |
| 65 | +} |
| 66 | + |
| 67 | +//완벽한 정답이 아닙니다. |
| 68 | +//정답 4 - chaerin-dev |
| 69 | +function solution(participant, completion) { |
| 70 | + var answer = ""; |
| 71 | + // 두 배열을 정렬한다! |
| 72 | + participant.sort(); |
| 73 | + completion.sort(); |
| 74 | + // 앞에서부터 차례로 비교하다가 값이 다를 때 participant의 요소가 완주하지 못한 선수!! |
| 75 | + // if (participant[i] != completion[i] || i == participant.length - 1) 이런 식으로 |
| 76 | + // 완주하지 못한 선수의 이름이 마지막에 있을 경우도 고려해야 하나..? 라고 생각했지만 |
| 77 | + // 그 때는 completion[i]의 값이 undefined가 되므로 괜찮음! |
| 78 | + for (let i = 0; i < participant.length; i++) { |
| 79 | + if (participant[i] != completion[i]) { |
| 80 | + answer = participant[i]; |
| 81 | + break; |
| 82 | + } |
| 83 | + } |
| 84 | + return answer; |
65 | 85 | }
|
0 commit comments