题解 | #提取不重复的整数#
提取不重复的整数
https://www.nowcoder.com/practice/253986e66d114d378ae8de2e6c4577c1
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 while ((line = await readline())) { let tokens = line.split(" "); tokens = tokens.toString().split("").reverse(); var myMap = new Map(); for (let i = 0; i < tokens.length; i++) { myMap.set(tokens[i]); } let myString = ""; myMap.forEach((val, key) => { myString += key; }) console.log(myString); } })();
我的方法
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 while ((line = await readline())) { let tokens = line.split(""); let arr = [...new Set(tokens.reverse())]; console.log(arr.join("")); } })();
一个大胖熊——的方法
用Set对象好,Set对象中存放的值不能重复,如果重复将不做操作。有几个特殊值需要特殊对待:
- +0 与 -0 在存储判断唯一性的时候是恒等的,所以不重复;
- undefined 与 undefined 是恒等的,所以不重复;
- NaN 与 NaN 是不恒等的,但是在 Set 中只能存一个,不重复。
练练练练练 文章被收录于专栏
练练练练练