Javascript: determine if all characters in a string are unique, and if not, remove duplicate characters

Create an array using a[letter][occurences] , but struggle with a loop through this array to check occurences > 1 and remove those that are.

 function charFreq(s) { var i, j; var a = new Array(); for (j = 0; j < s.length; j++) { for (i = 0; i < a.length; i++) { if (a[i][0] == s[j]) { a[i][1]++; break; } } if (i == a.length) { a[i] = [s[j], 1]; } } return a[i][0]; } document.write(charFreq("insert string here")); 

This is the mess I've come up with so far:

 function check(str) { var c; for (c=0; c < a.length; c++) { if(a[c][1] == 1) { return true; break; } else { return false; } } } 
+4
source share
7 answers

Do not do that.

 function noDups( s ) { var chars = {}, rv = ''; for (var i = 0; i < s.length; ++i) { if (!(s[i] in chars)) { chars[s[i]] = 1; rv += s[i]; } } return rv; } alert(noDups("Shoe fly pie, and apple pan dowdy")); // Shoe flypi,andw 

As the length of your string increases, your code becomes slower, approximately equal to the square of the length of the string.

+1
source

Using ES6 Set:

 // :: unique = Array<any>|string => Array<any> const unique = xs => [...new Set(xs)] const dedupe = str => unique(str).join('') console.log( unique('foo'), // => ['f', 'o'] dedupe('foo'), // => 'fo' ) 
+3
source

To remove duplicate characters from a string, you can use the following function that made user @Cerbrus

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

If you only want to return characters that appear once in a string, check to see if their last occurrence is in the same position as their first occurrence.

Your code returned all characters in a string at least once, instead of returning characters that occur no more than once

Stackoverflow stream link Remove duplicate characters from string

0
source

Here is a quick way:

 str = str.split('').filter(function(v,i,self){ return self.indexOf(v) == i; }).join(''); 
0
source
 function RemoveDuplicateLetters(input) { var result = '', i = 0, char = ''; while (i < input.length) { char = input.substring(i, i+1); result += char; input = input.replace(char,''); } return result; } 
0
source

I do not see the version for splicing, so here is one:

 function uniqueChars(s) { var s = s.split(''); var c, chars = {}, i = 0; while ((c = s[i])) { c in chars? s.splice(i, 1) : chars[c] = ++i; } return s.join(''); } 
0
source

This assumes only alpha characters, and uppercase is not equal to lowercase.

 function uniqueChars(string){ var i= 0, L= string.length, ustring= '', next; while(i<L){ next= string.charAt(i++); if(ustring.indexOf(next)== -1) ustring+= next; } return ustring.replace(/[^a-zA-Z]/g, ''); } var s1= 'The quick red fox jumps over the lazy brown dog.'; uniqueChars(s1) 

/ * return value: (String) Thequickrdfoxjmpsvtlazybwng * /

This returns any unique character -

 function uniqueArray(array){ return array.filter(function(itm, i, T){ return T.indexOf(itm)== i; }); } var s1= 'The quick red fox jumps over the lazy brown dog.'; uniqueArray(s1.split('')).join(''); 

/ * return value: (String) Quickrdfoxjmpsvtlazybwng. * /

0
source

Source: https://habr.com/ru/post/1487966/


All Articles