-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7kyu-string-matchup.js
More file actions
50 lines (36 loc) · 1.08 KB
/
Copy path7kyu-string-matchup.js
File metadata and controls
50 lines (36 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// CODEWARS 7KYU - STRING MATCHUP
// INSTRUCTIONS
// 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!
// If you like this Kata, please try:
// Word values
// Non-even substrings
// ALGORITHMS
// SOLUTION:
const solve =(arr1,arr2) => arr2.map(a=> arr1.filter(e=> e===a).length)
//OTHER SOLUTIONS:
function solve(a, b) {
return b.map(x => a.reduce((n, y) => x === y ? n + 1 : n, 0))
}
function solve(a,b){
let arrResult = [];
let cont = 0;
for (let i = 0; i < b.length; i++){
for (let j = 0; j < a.length; j++){
if (b[i] === a [j]){
cont++;
}
}
arrResult.push(cont);
cont = 0;
}
return arrResult;
}