题解 | #明明的随机数#
明明的随机数
http://www.nowcoder.com/practice/3245215fffb84b7b81285493eae92ff0
let N = parseInt(readline());
let numArr = [];
for (let i = 0; i < N; i++) {
const lineNum = parseInt(readline());
if (lineNum >= 1 && lineNum <= 500) numArr.push(lineNum);
}
function func(numArr) {
// 方法1:利用ES6 的 Set 去重
// const resArr = [...new Set(numArr)];
// 方法2:利用obj的key唯一特性去重
const resArr = toSetArr(numArr);
// sort排序
resArr.sort((a, b) => a-b);
resArr.forEach((item) => {
print(item);
});
}
// 利用obj的key唯一特性去重
function toSetArr(numArr) {
const resArr2 = [];
const map = {};
numArr.forEach((item) => {
if (!map[item]) {
map[item] = 1;
}
});
for (let key in map) {
resArr2.push(parseInt(key));
}
return resArr2;
}
func(numArr);