Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,870 questions

51,793 answers

573 users

How to check whether two strings contain same characters in JavaScript

2 Answers

0 votes
function sort_string(s) {
    var arr = s.split('');

    arr = arr.sort();
  
    return arr.join('');
}

function contain_same_characters(s1, s2) {
    s1 = sort_string(s1);
    s2 = sort_string(s2);
    
    return s1 === s2;
}
 
 
var s1 = "javascript programming";
var s2 = "scriptjava mmingprogra";
 
if (contain_same_characters(s1, s2)) {
    document.write("yes"); 
}
else {
    document.write("no"); 
}


/*
run:
    
yes 
 
*/

 



answered Oct 28, 2019 by avibootz
0 votes
function remove_duplicate_characters(s) {
  return s
    .split('')
    .filter(function(item, pos, self) {
      return self.indexOf(item) === pos;
    })
    .join('');
}

function sort_string(s) {
    var arr = s.split('');

    arr = arr.sort();
  
    return arr.join('');
}

function contain_same_characters(s1, s2) {
    s1 = sort_string(s1);
    s2 = sort_string(s2);
    
    return s1 === s2;
}
 
 
var s1 = "javascript programming";
var s2 = "scccriptjavaaaa mmmmmingprogggggggggra";

s1_tmp = remove_duplicate_characters(s1);
s2_tmp = remove_duplicate_characters(s2);
 
if (contain_same_characters(s1_tmp, s2_tmp)) {
    document.write("yes"); 
}
else {
    document.write("no"); 
}


/*
run:
    
yes 
 
*/

 



answered Oct 28, 2019 by avibootz

Related questions

...