查找字符串中不重复的字符

编程入门 行业动态 更新时间:2024-10-15 16:20:34
本文介绍了查找字符串中不重复的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我必须在JavaScript中创建一个函数,以删除字符串中所有重复的字母.到目前为止,我已经能够做到这一点:如果我有"anaconda"一词,则应在显示"cod"时将其显示为"anaconda".这是我的代码:

I have to make a function in JavaScript that removes all duplicated letters in a string. So far I've been able to do this: If I have the word "anaconda" it shows me as a result "anaconda" when it should show "cod". Here is my code:

function find_unique_characters( string ){ var unique=''; for(var i=0; i<string.length; i++){ if(unique.indexOf(string[i])==-1){ unique += string[i]; } } return unique; } console.log(find_unique_characters('baraban'));

推荐答案

我们现在还可以使用过滤器方法清理内容:

We can also now clean things up using filter method:

function removeDuplicateCharacters(string) { return string .split('') .filter(function(item, pos, self) { return self.indexOf(item) == pos; }) .join(''); } console.log(removeDuplicateCharacters('baraban'));

工作示例:

更多推荐

查找字符串中不重复的字符

本文发布于:2023-11-30 14:00:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1650251.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   中不   字符

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!