JavaScript:字符串包含多少个唯一字符?

给定az范围内的字符串。获取字符串中唯一的最少字符数。如果我们有一个字符串dabbcabcd,这里的最小数字将为4,因为这些字符列表不是相同的 'a'、'b'、'c'、'd'。

这是我累了


let output = [];

function fewestCoins(coins) {

for(let i = 0; i< coins.length; i++){

    if(coins[i] === coins[i+1]){

      output.push(coins[i])

    } else console.log(coins[i])

 }

}


let str = 'dabbcabcd';


fewestCoins(str);


慕的地8271018
浏览 161回答 2
2回答

炎炎设计

这是一种不同的方法,它将键的数量作为度量function fewestCoins(str) {&nbsp; g={}&nbsp; str=str.toLowerCase()&nbsp; &nbsp; for (let char of str) {&nbsp; &nbsp; &nbsp; g[char]=0&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; return Object.keys(g).length&nbsp; }&nbsp; console.log(fewestCoins('dabbcabcd'));

吃鸡游戏

您可以从字符串创建 aSet并获取其大小,因为Sets 仅包含唯一值。function fewestCoins(coins) {&nbsp; return new Set(coins).size;}let str = 'dabbcabcd';console.log(fewestCoins(str));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript