Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 039d400

Browse files
Add answer '완주하지 못한 선수'
1 parent 43bc643 commit 039d400

File tree

1 file changed

+57
-25
lines changed

1 file changed

+57
-25
lines changed

‎level-1/완주하지-못한-선수.js

Lines changed: 57 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,64 @@
22
//완벽한 정답이 아닙니다.
33
//정답 1 - codeisneverodd
44
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;
1313
}
1414

1515
//정답 2 - jaewon1676
1616
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-
}
31-
32-
return participant[0]
33-
}
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+
32+
return participant[0];
33+
}
34+
35+
//완벽한 정답이 아닙니다.
36+
//정답 3 - hyosung
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;
65+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /