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.
although the actual solution has nothing to do with the text, if you put the words in the right order it says:
to be or not to be that is the question
here's the code:
functionprocessData(input){// strip out the strings and convert; only numbers matterconstinputArr=input.split("\n").map((el)=>el.split(" "));constn=parseInt(inputArr.shift()[0]);// Not usedconstnumberArr=inputArr.map((el)=>parseInt(el[0]));// const stringArray = inputArr.map(el => el[1]); // Not used// Finding the frequency of each number in the arrayconstindexArray=newArray(100).fill(0);constfrequencyArray=numberArr.reduce((acc,el)=>{acc[Number.parseInt(el)]++;returnacc;},indexArray);// Creating the cumulative frequency arrayconstbigL=frequencyArray.reduce((acc,el,index,arr)=>{letcount=0;for(leti=0;i<index+1;i++){count+=arr[i];}acc.push(count);returnacc;},[]);console.log(bigL.join(" "));}
to put the words in order, add this:
// Putting the words in the correct orderconstsortedList=frequencyArray.reduce((acc,el,index)=>{if(el>0){for(leti=0;i<el;i++){acc.push(index);}}returnacc;},[]);constinputMap=newMap();for(letelofinputArr){if(inputMap.has(el[0])){inputMap.set(el[0],[...inputMap.get(el[0]),el[1]]);}else{inputMap.set(el[0],[el[1]]);}}constsortedWordsArray=sortedList.reduce((acc,el)=>{constwordValue=inputMap.get(`${el}`);if(wordValue.length>1){acc.push(wordValue.shift());inputMap.set(`${el}`,wordValue);}else{acc.push(wordValue[0]);inputMap.delete(`${el}`);}returnacc;},[]);console.log(sortedWordsArray.join(" "));
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Counting Sort 3
You are viewing a single comment's thread. Return to all comments →
javascript solution
although the actual solution has nothing to do with the text, if you put the words in the right order it says:
here's the code:
to put the words in order, add this: