Have the function FindIntersection(strArr) read the array of strings stored in strArr which will contain 2 elements: the first element will represent a list of comma-separated numbers sorted in ascending order, the second element will represent a second list of comma-separated numbers (also sorted). Your goal is to return a comma-separated string containing the numbers that occur in elements of strArr in sorted order. If there is no intersection, return the string false.
For example: if strArr contains ["1, 3, 4, 7, 13", "1, 2, 4, 13, 15"] the output should return "1,4,13" because those numbers appear in both strings. The array given will not be empty, and each string inside the array will be of numbers sorted in ascending order and may contain negative numbers.
Input: ["1, 3, 4, 7, 13", "1, 2, 4, 13, 15"] Output: 1,4,13
Input: ["1, 3, 9, 10, 17, 18", "1, 4, 9, 10"] Output: 1,9,10
Worst case:
function FindIntersection(strArr) { // code goes here const [a, b] = strArr; const nunbers = a.split(',').filter(el => b.split(',').includes(el)); return numbers.length ? numbers.map(el => el.trim()).join(',') : false; } // keep this function call here console.log(FindIntersection(readline()));
Best case:
function FindIntersection(strArr) { // code goes here const [a, b] = strArr.map(s => s.split(', ')); const numMap = {}; const numbers = []; for (let number of a) { numMap[number] = number; } for (let number of b) { if (numMap[number]) { numbers.push(number); } } return numbers.length ? numbers.join(',') : false; }