Find the Length of the Longest possible palindrome string JavaScript

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

Given a string s which consists of lowercase or uppercase letters, we are required to return the length of the longest palindrome that can be built with those letters. Letters are case sensitive, for example, "Aa" is not considered a palindrome here.

For example −

If the input string is −

const str = "abccccdd";

then the output should be 7,

because, one longest palindrome that can be built is "dccaccd", whose length is 7.


const str = "abccccdd";
const longestPalindrome = (str) => {
   const set = new Set();
   let count = 0;
   for (const char of str) {
      if (set.has(char)) {
         count += 2; set.delete(char);
      else {
   return count + (set.size > 0 ? 1 : 0);


And the output in the console will be −

Published on 21-Nov-2020 10:23:48