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.
Awesome solution. I actually didn't think of it this way. I have come up with my own solution basing on yours.
functionprocessData(input){//Enter your code herevarnumCounts=[];// Will hold the number of counts.varsortedArray=[];// Will hold the sorted arrayinput=input.split("\n");// Split to get the inputs.letelements=input[1].split(" ").map(Number);elements.forEach(function(value){// Using the array elements as keys and counts as values.numCounts[value]=numCounts[value]+1||1;});// Loop through the array.for(vari=0;i<numCounts.length;i++){// Each index of numCounts has a value count.// This loop inserts into the sortedArray depending on the countsfor(varj=0;j<numCounts[i];j++){sortedArray.push(i);}}console.log(sortedArray.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 2
You are viewing a single comment's thread. Return to all comments →
Awesome solution. I actually didn't think of it this way. I have come up with my own solution basing on yours.