I am trying to solve algorithm problem in pseint pseudocode program, the problem is as follows:
How can I count how many times each of the names in a names list is repeated? Does anyone have any idea how it could be done?
I know how to do it for 1 single value(as long as I know it), but I can't identify how I could adapt this for what I'm looking for
DIVIDE ET IMPERA your problem :)
find the distinct names in your list
foreach element of the previous step, count how many times it is in your list.
Find the distinct name in your list:
//create a new array and suppose it has as many elements as the given input:
let ris = new array[input.length]
//define an index for this array.
let k = 0
//in the rest of the algorithm let always k be the actual length of ris and the next possible index to use
//let's start iterating over the input list
for (i = 0; i<input.length; i++) {
//let's check if input[i] it's already present in ris
//we can look just for the first k elements
let addCurrentName = true
for (j = 0; j<k; j++) {
if (input[i] == ris[j]) addCurrentName = false;
}
if (addCurrentName) {
//we go inside this if only if in the previous search we found nothing
ris[k] = input[i];
k++;
}
}
//now the first k positions of ris contain the distinct values of input
let ris2 = new array[k] //now we are sizing the result exactly
for (i = 0; i<k; i++) {
ris2[i] = ris[i] //we are just creating the result of the right dimension and copying values into it
}
return ris2;
You said you already know how to count how many times an element is in an array, so I'll leave the rest to you, you just have to do it for all the distinct values.