We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
functioncountOccurrences(arr){constoccurrences={};// Create an empty object to store element occurrencesfor(leti=0;i<arr.length;i++){// Iterate through each element in the arrayconstelement=arr[i];// Get the current elementif(occurrences[element]){// If the element already exists as a key in occurrencesoccurrences[element]+=1;// Increment the count for that element by 1}else{// If the element doesn't exist as a key in occurrencesoccurrences[element]=1;// Create a new key for the element with an initial count of 1}}returnoccurrences;// Return the occurrences object}functioncheckPalindrome(text){letnumberOfUnevenWords=0;// Initialize a counter for the number of unevenly occurring charactersconsttextArr=text.split('').sort();// Convert the input text into an array of characters and sort itconstcountObj=countOccurrences(textArr);// Count the occurrences of each character using the countOccurrences functionfor(letkeyincountObj){// Iterate over each unique character in the countObjnumberOfUnevenWords+=countObj[key]%2;// Increment the numberOfUnevenWords if the count of the character is odd}returnnumberOfUnevenWords<=1;// Return true if the numberOfUnevenWords is less than or equal to 1, indicating a palindrome}/* * Complete the 'gameOfThrones' function below. * * The function is expected to return a STRING. * The function accepts STRING s as parameter. */functiongameOfThrones(s){// Write your code hereconstisPalindrome=checkPalindrome(s)returnisPalindrome?'YES':'NO'}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Game of Thrones - I
You are viewing a single comment's thread. Return to all comments →
JavaScript solution with detailed comments: