Read Time:57 Second
Complete the countingSort function in the editor below.
countingSort has the following parameter(s):
- arr[n]: an array of integers
Returns
- int[100]: a frequency array
'use strict';
const fs = require('fs');
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', function(inputStdin) {
inputString += inputStdin;
});
process.stdin.on('end', function() {
inputString = inputString.split('\n');
main();
});
function readLine() {
return inputString[currentLine++];
}
/*
* Complete the 'countingSort' function below.
*
* The function is expected to return an INTEGER_ARRAY.
* The function accepts INTEGER_ARRAY arr as parameter.
*/
function countingSort(arr) {
let counts=[];
// n case below
for(let i=0;i<100;i++){
counts[i]=0;
}
for(let j=0;j<arr.length;j++){
if(arr[j]<100){
counts[arr[j]]+=1;
}
}
return counts;
}
function main() {
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
const n = parseInt(readLine().trim(), 10);
const arr = readLine().replace(/\s+$/g, '').split(' ').map(arrTemp => parseInt(arrTemp, 10));
const result = countingSort(arr);
ws.write(result.join(' ') + '\n');
ws.end();
}
Function Description