저장을 습관화
프로그래머스 LV.0 배열의 유사도 본문
프로그래머스 LV.0 배열의 유사도
https://school.programmers.co.kr/learn/courses/30/lessons/120903
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
1. 문제 명
배열의 유사도
2. 문제 설명
두 배열이 얼마나 유사한지 확인해보려고 합니다. 문자열 배열 s1과 s2가 주어질 때 같은 원소의 개수를 return하도록 solution 함수를 완성해주세요.
3. 제한 사항
- 1 ≤ s1, s2의 길이 ≤ 100
- 1 ≤ s1, s2의 원소의 길이 ≤ 10
- s1과 s2의 원소는 알파벳 소문자로만 이루어져 있습니다
- s1과 s2는 각각 중복된 원소를 갖지 않습니다.
4. 예시
s1 | s2 | result |
["a", "b", "c"] | ["com", "b", "d", "p", "c"] | 2 |
["n", "omg"] | ["m", "dot"] | 0 |
5. 기본 제공 코드
function solution(s1, s2) {
var answer = 0;
return answer;
}
6. 제출한 내 답
const solution = (s1, s2) => {
let answer = 0;
s1.forEach((s1El) => {
s2.forEach((s2El) => {
if (s1El === s2El) answer += 1;
});
});
return answer;
};
6-2. VSC에 작성한 내용
const solution = (s1, s2) => {
let answer = 0;
s1.forEach((s1El) => {
s2.forEach((s2El) => {
if (s1El === s2El) answer += 1;
});
});
return answer;
};
// 테스트
console.log(solution(["a", "b", "c"], ["com", "b", "d", "p", "c"]));
console.log(solution(["n", "omg"], ["m", "dot"]));
7. 특이사항
forEach 문을 연습하기 좋은 문제였다
8. 다른 사람이 작성한 답
8-1.
function solution(s1, s2) {
const intersection = s1.filter((x) => s2.includes(x));
return intersection.length;
}
뭐가 forEach 연습하기 좋은 문제야 filter include도 떠올리지 못하는 허접이
8-2.
function solution(s1, s2) {
return s1.filter((v) => s2.includes(v)).length;
}
8-3.
function solution(s1, s2) {
const concat = [...s1, ...s2];
const setConcat = Array.from(new Set(concat));
return concat.length - setConcat.length;
}
8-4. for of
function solution(s1, s2) {
let count = 0;
for (let v of s1) if (s2.includes(v)) count++;
return count;
}
'코딩 테스트 > 프로그래머스 - 자바스크립트' 카테고리의 다른 글
프로그래머스 LV.0 편지 (0) | 2023.09.10 |
---|---|
프로그래머스 LV.0 순서쌍의 개수 (0) | 2023.09.10 |
프로그래머스 LV.0 점의 위치 구하기 (0) | 2023.09.09 |
프로그래머스 LV.0 문자열 뒤집기 (0) | 2023.09.09 |
프로그래머스 LV.0 아이스 아메리카노 (0) | 2023.09.09 |