(23)Symmetric Difference
23-ий день программы "#100 Days Of Code"
function sym(args) { return Array.prototype.reduce.call(arguments,(a,b)=>a.filter(c=>b.every(d=>c!=d)).concat(b.filter(c=>a.every(d=>c!=d))),[]).filter((e,i,a)=>a.indexOf(e)===i); } sym([1, 2, 3], [5, 2, 1, 4]);
const sym = (...args) => { return args.reduce((acc, curr) => { // change curr to hold unique values const set = new Set(curr) curr = Array.from(set) return acc.filter(a => !curr.includes(a)).concat(curr.filter(b => !acc.includes(b))) }, []) } sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5])
Ответы:
sym([1, 2, 3], [5, 2, 1, 4])
return [3, 4, 5].
sym([1, 2, 3], [5, 2, 1, 4])
contain only three elements.
sym([1, 2, 5], [2, 3, 5], [3, 4, 5])
return [1, 4, 5]
sym([1, 2, 5], [2, 3, 5], [3, 4, 5])
contain only three elements.
sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5])
return [1, 4, 5].
sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5])
contain only three elements.
sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3])
return [2, 3, 4, 6, 7].
sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3])
contain only five elements.
sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1])
return [1, 2, 4, 5, 6, 7, 8, 9].
sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1])
contain only eight elements.
Комментариев нет:
Отправить комментарий