CodeWars 코딩 문제 2021/01/20 - String matchup

이호현·2021년 1월 20일
0

Algorithm

목록 보기
57/138

[문제]

Given two arrays of strings, return the number of times each string of the second array appears in the first array.

Example
array1 = ['abc', 'abc', 'xyz', 'cde', 'uvw']
array2 = ['abc', 'cde', 'uap']
How many times do the elements in array2 appear in array1?

'abc' appears twice in the first array (2)
'cde' appears only once (1)
'uap' does not appear in the first array (0)
Therefore, solve(array1, array2) = [2, 1, 0]

Good luck!

(요약) array2의 각 요소가 array1의 요소에서 몇번 나오나 찾기.

[풀이]

function solve(a,b){
  return b.map(str => {
    let count = 0;
    a.forEach(str2 => str === str2 && count++);
    return count;
  });
}

두 번째 배열을 map을 이용해 바로 개수를 구함.
mapcallback으로 첫 번째 배열을 forEach로 반복해서 카운트 된 개수 return.

profile
평생 개발자로 살고싶습니다

0개의 댓글