Member-only story
Coding Interview Prep: Given a list of integers, count and return the number of times each value appears as an array of integers
Jun 27, 2024
Prompt
Solution
The return array should include the number of times each value appears in the parameter array. The result array will be as so,
- First index is how many times 0 appears in the array
- Second index is how many times 1 appears in the array
- Third index is how many times 2 appears in the array
- And so on…
function find(toFind, arr){
let count = 0;
for (let j=0; j<arr.length; j++){
if (toFind == arr[j]){
count++;
}
}
return count;
}
function countingSort(arr) {
const res = [];
for (let i=0; i<100; i++){
const count = find(i, arr);
res.push(count);
}
return res;
}