Sorted Union
function uniteUnique(arr) {
//合并数组
//刷选唯一
let allInOne= [...arguments].reduce((arrA, arrB)=> [...arrA, ...arrB])
console.log(`allInOne: ${allInOne}`)
let sortedUnionArr= allInOne.reduce((array, item)=> {
console.log(array+item+array.includes(item))
return array.includes(item)?array: [...array, item]
}, [])
return sortedUnionArr
}
uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]);
测试数据:
uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]) should return [1, 3, 2, 5, 4].
uniteUnique([1, 2, 3], [5, 2, 1]) should return [1, 2, 3, 5].
uniteUnique([1, 2, 3], [5, 2, 1, 4], [2, 1], [6, 7, 8]) should return [1, 2, 3, 5, 4, 6, 7, 8].
//别人家的孩子
function uniteUnique(...arrays) {//接收所有参数,放进arrays 数组中
//make an array out of the given arrays and flatten it (using the spread operator)
//concat 可以一口气连接多个数组。[].concat(arr1, arr2, arr3)
const flatArray = [].concat(...arrays);
// create a Set which clears any duplicates since it's a regulat set and not a multiset
//new Set()返回的是对象{},对象转换成数组[...obj]
return [...new Set(flatArray)];
}
// test here
uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]);