Open this example in the interactive library
Source code
'use strict';
import { WriteStream, createWriteStream } from "fs";
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString: string = '';
let inputLines: string[] = [];
let currentLine: number = 0;
process.stdin.on('data', function(inputStdin: string): void {
inputString += inputStdin;
});
process.stdin.on('end', function(): void {
inputLines = inputString.split('\n');
inputString = '';
main();
});
function readLine(): string {
return inputLines[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: number[]): number[] {
// Initialize a frequency array with 100 elements, all set to 0
const freqArray: number[] = new Array(100).fill(0);
// Count occurrences of each integer in the input array and update the frequency array
for (const num of arr) {
freqArray[num]++;
}
return freqArray;
}
function main() {
const ws: WriteStream = createWriteStream(process.env['OUTPUT_PATH']);
const n: number = parseInt(readLine().trim(), 10);
const arr: number[] = readLine().replace(/\s+$/g, '').split(' ').map(arrTemp => parseInt(arrTemp, 10));
const result: number[] = countingSort(arr);
ws.write(result.join(' ') + '\n');
ws.end();
}
