在处理分类问题的时候,Map总能起到效果
let str = [3, 12, 5, 10, 4, 31, 3, 2, 3, 5, 6, 4, 5, 8, 3, 4, 2];
let group = 0;
let data = new Map();
let last = 0;
let result=[]
str.forEach((item, index) => {
const arr = data.get(group);
if (index == 0 || (arr.length>= last)) {
last = item;
group = last + group+1
data.set(group, []);
} else if (index <= group) {
data.get(group).push(item);
}
});
data.forEach(item=>{
result.push(...Array.from(new Set(item)).sort((a,b)=>a-b))
})
console.log(result)