前端算法之重复字符串

function fn(str) {
  let obj = {};
  let temp = null
  for (let i = 0; i < str.length; i++) {
    temp = str[i];
    if (obj[temp]) {
      obj[temp] = obj[temp] + 1;
    } else {
      obj[temp] = 1;
    }
  }
  console.log(obj);
  let result = [];
  for (let i = 0; i < str.length; i++) {
    temp = str[i];
    if (obj[temp] === 1) {
      result.push(temp);
    } else {
      result.push("#");
    }
  }
  console.log(result.join(''));
}
fn("google");
posted @ 2022-03-14 13:37  jerry-mengjie  阅读(65)  评论(0)    收藏  举报