题解 | #数据分类处理#
数据分类处理
https://www.nowcoder.com/practice/9a763ed59c7243bd8ab706b2da52b7fd
const rl = require("readline").createInterface({ input: process.stdin }); var iter = rl[Symbol.asyncIterator](); const readline = async () => (await iter.next()).value; void (async function () { // Write your code here let index = 0; let arrI = [], arrR = []; while ((line = await readline())) { if (index === 0) { arrI = line.split(" "); arrI.shift(); index++; } else { arrR = line.split(" "); arrR.shift(); } } arrR = Array.from(new Set(arrR)); arrR.sort((a, b) => a - b); let hashArray = new Map(); for (let r = 0; r < arrR.length; r++) { let indexArr = []; let itemArr = []; let obj; for (let i = 0; i < arrI.length; i++) { if (arrI[i].includes(arrR[r])) { indexArr.push(i); itemArr.push(arrI[i]); if (hashArray.has(arrR[r])) { obj.num += 1; obj.index = indexArr; obj.item = itemArr; } else { obj = { num: 1, index: indexArr, item: itemArr, }; } hashArray.set(arrR[r], obj); } } } let key = hashArray.keys(); let finalArray = []; while ((k = key.next().value)) { let value = hashArray.get(k); finalArray.push(k); finalArray.push(value.num); for (let i = 0; i < value.index.length; i++) { finalArray.push(value.index[i]); finalArray.push(value.item[i]); } } let len = finalArray.length.toString(); console.log(len.concat(" ", finalArray.join(" "))); })();