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

## 没有添加任何判断只是为了实现需求 不知道自己写的对不对 看上面大家都写了好多 是不是我理解错了啊 #1194

Closed
Evllis opened this issue Aug 9, 2021 · 0 comments

Comments

@Evllis
Copy link

Evllis commented Aug 9, 2021

如果碰到多维数据就废了,比如:
let arr = [1, 2, [3, 4, [5, 6, 7, 3, [10, 12]], 7, 8]];
当然你这个解题对二维数组是完全可以解决的,但我觉得应该尽量想的周全一点。
加油。

没有添加任何判断只是为了实现需求 不知道自己写的对不对 看上面大家都写了好多 是不是我理解错了啊

  • 先利用flat进行扁平化处理
  • 然后利用Set对象进行去重处理
  • 利用from将set数组化
  • 最后利用sort排序
 let arr = [
      [1, 4, 6],
      [7, 8, 10],
      [2, 6, 9],
      [3, 7, 13],
      [1, 5, 12]
    ];
//[[1,2,3],[4,5,6],[7,8,9],[1,2,3],[4,5,6]]

    function func(arr) {
      return Array.from(new Set(arr.flat(1))).sort((a, b) => a - b)
    }

//[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13]
// [1, 2, 3, 4, 5, 6, 7, 8, 9]

Originally posted by @azhen98 in #8 (comment)

@Evllis Evllis closed this as completed Aug 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant