Java: Print a unique character in a string

前端 未结 17 1347
长情又很酷
长情又很酷 2021-01-03 00:03

I\'m writing a program that will print the unique character in a string (entered through a scanner). I\'ve created a method that tries to accomplish this but I keep getting

17条回答
  •  庸人自扰
    2021-01-03 00:48

    Here str will be your string to find the unique characters.

    function getUniqueChars(str){
    let uniqueChars = '';
    for(let i = 0; i< str.length; i++){
      for(let j= 0; j< str.length; j++) {
        if(str.indexOf(str[i]) === str.lastIndexOf(str[j])) {
           uniqueChars += str[i];
         }
       }
     }
     return uniqueChars;    
    

    }

提交回复
热议问题