Skip to content
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题(2019-07-26) 已知以下数组: 编写一个程序将数组扁平化去并除其中重复部分数据,最终得到一个升序并且不重复的数组 #21

Open
qappleh opened this issue Aug 19, 2019 · 1 comment
Labels

Comments

@qappleh
Copy link
Owner

qappleh commented Aug 19, 2019

var arr = [ [11, 22, 22], [13, 14, 15, 15], [16, 17, 18, 19, [11, 12, [12, 13, [14] ] ] ], 12];
@qappleh
Copy link
Owner Author

qappleh commented Aug 19, 2019

参考答案:

//flat方法接受一个参数n,可以将深度为n的数组扁平化,然后去重并排序,去重可以用set,排序用sort
// 当然也可以使用递归等其他方法
var newArray = Array.from(new Set(arr.flat(Infinity))).sort((a,b) => a-b);
console.log(newArray);    

@qappleh qappleh added the js label Nov 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant