-
Notifications
You must be signed in to change notification settings - Fork 634
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
腾讯:数组扁平化、去重、排序 #5
Comments
补充:flat() 方法对node版本有要求,至少需要12.0以上,不知道的小伙伴会搞错,然后我的方法是 let arr =[[1, 2, 2], [3, 4, 5, 5], [6, 7, 8, 9, [11, 12, [12, 13, [14]]]], 10]
function newArray ( ) {
while(arr.some(Array.isArray)){
arr = [].concat(...arr)
}
arr = [...new Set(arr)].sort((a,b)=>{
return a-b
})
return arr
}
newArray()
console.log(arr); |
给个递归方法: const ans = [];
var flaten = function (nums) {
if (typeof nums == 'number') {
return nums;
} // 出口
nums.forEach(element => {
let tmp = flaten(element);
if (tmp != null) {
ans.push(tmp);
}
});
} ans是作为全局变量的,也可以作为参数传递。 |
|
const getArray = (arr, dep) => Array.from(new Set(arr.flat(dep))).sort() |
考虑到这儿有三个步骤,可以用函数组合compose来把这三个函数组合在一起。 \\ 数组扁平化
var _flatten = function (array) {
return array.reduce(function (prev, next) {
return prev.concat(Array.isArray(next) ? _flatten(next) : next)
}, [])
}
\\ 数组去重
var _unique = function (array) {
var map = {}
return array.filter(function (item, index, array) { // 用typeof item +JSON.stringfy(item)的原因是用来区分两个对象
return map.hasOwnProperty(typeof item + JSON.stringify(item)) ? false : map[typeof item + JSON.stringify(item)] = true
})
}
\\ 数组排序
var _sort = function (array) {return array.sort((a, b) => a - b)}
\\ 函数组合
var compose = (...fns) => (initValue) => fns.reduceRight((y, fn) => fn(y), initValue)
var flatten_unique_sort = compose( _sort, _unique, _flatten)
var array = [ [1, 2, 2], [3, 4, 5, 5], [6, 7, 8, 9, [11, 12, [12, 13, [14] ] ] ], 10]
console.log(flatten_unique_sort(array)) |
This looks so cool!But,it has a mistake。
|
/**
* 数组扁平化、去重、排序
*/
const list = [1, [2, 3, 8], [3, 6, 4], [5, [6, 7, [8, 1, 2]]]];
/* ====== 扁平化 ====== */
function flat(arr) {
return arr.reduce((acc, cur) => {
acc = acc.concat(Array.isArray(cur) ? flat(cur) : cur);
return acc;
}, []);
}
/* ====== 数组去重 ====== */
function unique(arr) {
return arr.reduce((acc, cur) => {
if (!acc.includes(cur)) acc.push(cur);
return acc;
}, []);
}
/* ====== 排序 ====== */
// 冒泡排序
function pupleSort(arr) {
for (let i = 0; i < arr.length; i++) {
for (let j = i; j < arr.length; j++) {
if (arr[i] > arr[j]) {
const temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
return arr;
}
// 快速排序1
function quickSort(arr) {
if (arr.length <= 1) return arr;
const left = [];
const right = [];
const mid = arr.shift(arr);
arr.forEach((item) => {
if (item <= mid) left.push(item);
else right.push(item);
});
return quickSort(left).concat(mid).concat(quickSort(right));
}
const flatArr = flat(list);
console.log('flatArr: ', flatArr); // [1, 2, 3, 8, 3, 6, 4, 5, 6, 7, 8, 1, 2]
const uniArr = unique(flatArr);
console.log('uniArr: ', uniArr); // [1, 2, 3, 8, 6, 4, 5, 7]
// const sortArr = pupleSort(uniArr); // [1, 2, 3, 4, 5, 6, 7, 8]
const sortArr = quickSort(uniArr); // [1, 2, 3, 4, 5, 6, 7, 8]
console.log('sortArr: ', sortArr); |
it works well for me . [2,6,4,8,1].sort()
// (5) [1, 2, 4, 6, 8] |
直接为sort()是根据Unicode来排序的 |
let array = [ [1, 2, 2], [3, 4, 5, 5], [6, 7, 8, 9, [11, 12, [12, 13, [14] ] ] ], 10] |
var arr = [[1,2,2],[3,4,5,5],[6,7,8,9,[11,12,[12,13,[14]]]],10] console.log(flat(arr)) |
var arr = [[1, 2, 2], [3, 4, 5, 5], [6, 7, 8, 9, [11, 12, [12, 13, [14]]]], 10];
/**
* 展平数组
* @param {number[]} array
*/
function flatten(array) {
let temp = array;
while (temp.some(Array.isArray)) {
temp = [].concat(...temp);
}
return temp;
}
/**
* 计数排序加去重
* @param {number[]} array
*/
function countSort(array) {
const max = Math.max.apply(null, array);
const min = Math.min.apply(null, array);
const d = max - min;
const countArray = Array(d + 1).fill(0);
array.forEach((num) => {
countArray[num - min]++;
});
return countArray.reduce((sorted, current, index) => {
if (current > 0) {
sorted.push(index + min);
}
return sorted;
}, []);
}
function sort(array) {
let temp = flatten(array);
return countSort(temp);
}
const res = sort(arr);
console.log(res);
// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]; |
// 数组扁平化
const flatten = (array) =>
array.reduce(
(prev, next) => prev.concat(Array.isArray(next) ? flatten(next) : next),
[]
)
// 数组去重
var unique = (array) =>
array.filter((item) =>
unique[uniqueKey(item)]
? false
: (unique[uniqueKey(item)] = true)
)
// uniqueKey 的值跟传入的 item 有关
// 数组排序
var sort = array => array.sort((a, b) => a - b)
// 函数组合
var compose = (...fns) => (...args) => fns.reduce((p, n) => n(p), args)
var flatten_unique_sort = compose( flatten, unique, sort )
var array = [ [1, 2, 2], [3, 4, 5, 5], [6, 7, 8, 9, [11, 12, [12, 13, [14] ] ] ], 10]
console.log(flatten_unique_sort(array)) |
|
const flattenUniqueSort = (array) => {
// 拍平
const flatten = (array) => {
return array.reduce((result, it) => {
return result.concat(Array.isArray(it) ? flatten(it) : it)
}, [])
}
// 去重
const unique = (array) => [ ...new Set(array) ]
// 排序
const sort = (array) => array.sort((a, b) => a - b)
return sort(unique(flatten(array)))
}
|
function flat(arr){
return [...new Set(arr.toString().split(",").map(Number).sort((a,b)=>a-b))]
} |
|
这样会不会更好 flat(arr) // [1, 1, 2, 1, 2, 3] |
function translateArr(arr) {
while(arr.some(Array.isArray)) {
arr = [].concat(...arr)
}
arr = [...new Set(arr)]
arr = arr.sort((a,b)=>a-b)
return arr
}
let arrE = flatArr([1,2,3,[4,1,2, 9,0],[1,2,[1,4,5, [3,5,6]]]]) |
var arr = [[1, 2, 3], [2, 3, 4, 4, 45, 5], [6, 7, 8, 5]]
function flatern1(arr = []) {
//利用toString的性质
let setArry = new Set(arr.toString().split(',').sort((a, b) => a - b))
return Array.from(setArry).map((value) => Number.parseInt(value))
}
//flat() 方法会按照一个可指定的深度递归遍历数组,并将所有元素与遍历到的子数组中的元素合并为一个新数组返回。
function flatern2(arr) {
return Array.from(new Set(arr.flat(4))).sort((a, b) => a - b)
}
console.log(flatern1(arr));
console.log(flatern2(arr)); |
回字的四种写法 // 扁平化 第一种
const flatten = (target) => {
if (Array.isArray(target)) {
return target.flat(Infinity);
}
};
// 扁平化 第二种
const flatten_0 = (target) => {
if (Array.isArray(target)) {
return target
.toString()
.split(",")
.map((ele) => Number(ele));
}
};
// 扁平化 第三种
const flatten_1 = (target, arrs) => {
arrs = arrs || [];
target.forEach((element) =>
Array.isArray(element) ? flatten_1(element, arrs) : arrs.push(element)
);
return arrs;
};
// 扁平化 第四种
const flatten_3 = (target) => {
if(Array.isArray(target)){
return target.reduce((add, ele)=>{
return add.concat(Array.isArray(ele) ? flatten_3(ele):ele)
}, [])
}
} |
这是来自QQ邮箱的假期自动回复邮件。你好,我最近正在休假中,无法亲自回复你的邮件。我将在假期结束后,尽快给你回复。
|
关于
Array
的属性、方法这里不再做介绍,详看 MDN Array 。面试题:
答案:
可结合 携程&蘑菇街&bilibili:手写数组去重、扁平化函数 查看
The text was updated successfully, but these errors were encountered: