Skip to content

第 11 题:将数组扁平化并去除其中重复数据,最终得到一个升序且不重复的数组 #8

Open
@zpzxgcr

Description

@zpzxgcr

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

Activity

zpzxgcr

zpzxgcr commented on Feb 12, 2019

@zpzxgcr
Author

image

zpzxgcr

zpzxgcr commented on Feb 12, 2019

@zpzxgcr
Author
相关知识点

es6数组常用操作

hejiaqian

hejiaqian commented on Feb 12, 2019

@hejiaqian
arr.toString().split(",").sort((a,b)=>{ return a-b})
hejiaqian

hejiaqian commented on Feb 12, 2019

@hejiaqian

image

zpzxgcr

zpzxgcr commented on Feb 12, 2019

@zpzxgcr
Author

arr.toString()。split(“,”)。sort((a,b)=> {return ab})

你可能后面需要 arr.toString().split(",").sort((a,b)=>{ return a-b}).map(Number)
不然数组元素都是字符串 结果并没有去重 还有两个2 5和12

hejiaqian

hejiaqian commented on Feb 12, 2019

@hejiaqian

arr.toString()。split(“,”)。sort((a,b)=> {return ab})

你可能后面需要 arr.toString().split(",").sort((a,b)=>{ return a-b}).map(Number)
不然数组元素都是字符串

谢谢大佬指点,学习了

changed the title [-]将数组扁平化并去除其中重复数据,最终得到一个升序且不重复的数组[/-] [+]第11题:将数组扁平化并去除其中重复数据,最终得到一个升序且不重复的数组[/+] on Feb 12, 2019
cjfff

cjfff commented on Feb 12, 2019

@cjfff
Array.prototype.flat= function() {
    return [].concat(...this.map(item => (Array.isArray(item) ? item.flat() : [item])));
}

Array.prototype.unique = function() {
    return [...new Set(this)]
}

const sort = (a, b) => a - b;

console.log(arr.flat().unique().sort(sort)); // [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 ]
AprilTong

AprilTong commented on Feb 12, 2019

@AprilTong
var arr = [ [1, 2, 2], [3, 4, 5, 5], [6, 7, 8, 9, [11, 12, [12, 13, [14] ] ] ], 10];
function flatten(arr) {

    while (arr.some(item => Array.isArray(item))) {
        arr = [].concat(...arr);
    }

    return arr;
}
Array.from(new Set(flatten(arr))).sort((a, b) => {
 return a - b
})
zpzxgcr

zpzxgcr commented on Feb 12, 2019

@zpzxgcr
Author
Array.prototype.falt = function() {
    return [].concat(...this.map(item => (Array.isArray(item) ? item.falt() : [item])));
}

Array.prototype.unique = function() {
    return [...new Set(this)]
}

const sort = (a, b) => a - b;

console.log(arr.falt().unique().sort(sort)); // [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 ]

函数名应该是flat

cjfff

cjfff commented on Feb 12, 2019

@cjfff
Array.prototype.falt = function() {
    return [].concat(...this.map(item => (Array.isArray(item) ? item.falt() : [item])));
}

Array.prototype.unique = function() {
    return [...new Set(this)]
}

const sort = (a, b) => a - b;

console.log(arr.falt().unique().sort(sort)); // [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 ]

函数名应该是flat

  • -感谢斧正
w587

w587 commented on Feb 12, 2019

@w587
var old_arr=[ [1, 2, 2], [3, 4, 5, 5], [6, 7, 8, 9, [11, 12, [12, 13, [14] ] ] ], 10];
		
// 数组拍平
var level_arr=old_arr.flat(4);

//数组去重
var Distinct=Array.from(new Set(level_arr));

// 排序
var sort=  Distinct.sort((a, b) =>a - b)

console.log("new arr",sort)
jackchenl

jackchenl commented on Feb 13, 2019

@jackchenl

递归函数解法:

function spreadArr(arr=[]){
	if(arr.some(ele=>Array.isArray(ele))){
		let newArr = [];
		arr.forEach((ele) => {
			if(Array.isArray(ele)){
				newArr = newArr.concat(...ele)
			}else{
				if(!newArr.includes(ele)) newArr.push(ele)
			}
		})
		return spreadArr(newArr);
	}
	return arr.sort((a,b)=> a-b);
}
spreadArr([ [1, 2, 2], [3, 4, 5, 5], [6, 7, 8, 9, [11, 12, [12, 13, [14] ] ] ], 10]);
zhuzhuoxingguang

zhuzhuoxingguang commented on Feb 14, 2019

@zhuzhuoxingguang

利用前面的答案,修正一个没用ES6的写法:

// 1、扁平化数组
var flatArr = arr.toString().split(",");
// 2、去重
var hash = {};
for (var i = 0, len = flatArr.length; i < len; i++) {
hash[flatArr[i]] = "abc"
}
flatArr = [];
// 3、将元素字符串转化为数字、遍历hash并不能保证输出顺序
for (var i in hash) {
flatArr.push(+i)
}
// 4、排序
flatArr = flatArr.sort(function(a, b) {
return a - b
})
console.log(flatArr)

sisterAn

sisterAn commented on Feb 16, 2019

@sisterAn
Collaborator

看一道面试题:

已知如下数组:var arr = [ [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]
// 扁平化
let flatArr = arr.flat(4)
// 去重
let disArr = Array.from(new Set(flatArr))
// 排序
let result = disArr.sort(function(a, b) {
    return a-b
})
console.log(result)
// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

关于 Set 请查阅 Set、WeakSet、Map及WeakMap

本文首发于我的博客:数组扁平化、去重、排序

398 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @hongz1125@miser@Pazzilivo@miniflycn@kerrysheng

        Issue actions

          第 11 题:将数组扁平化并去除其中重复数据,最终得到一个升序且不重复的数组 · Issue #8 · Advanced-Frontend/Daily-Interview-Question