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,938 questions

51,875 answers

573 users

How to remove odd frequency characters from a string in JavaScript

1 Answer

0 votes
function removeOddFrequencyCharacters(s) { 
    const frequencyMap = new Map();
    const chArray = Array.from(s);
 
    // Count the frequency of each character
    for (const ch of chArray) {
        frequencyMap.set(ch, (frequencyMap.get(ch) || 0) + 1);
    }
 
    // Build the result string excluding characters with odd frequencies
    let result = '';
    for (const ch of chArray) {
        if (frequencyMap.get(ch) % 2 === 0) {
            result += ch;
        }
    }
 
    return result; 
} 
     
let s = "javascript programming version 15 ECMAScript 2024"; 

s = removeOddFrequencyCharacters(s);

console.log(s);

    
      
      
/*
run:
      
vscitogmmingvsioncit22
      
*/

 



answered Nov 20, 2024 by avibootz

Related questions

1 answer 91 views
1 answer 98 views
1 answer 88 views
1 answer 79 views
1 answer 83 views
1 answer 81 views
...