Basically, that code does this:
1) Initializes temp array with 100 positions (all set to 0);
2) Iterates over data array and increments the value relative to the index of the data array value it’s processing. Take this example:
int[] data = {23,11,10,23,5};
temp[data[0]] += 1
temp[23] += 1
temp[23] = temp[23] + 1 = 0 + 1 = 1
temp[data[1]] += 1
temp[11] += 1
temp[11] = temp[11] + 1 = 0 + 1 = 1
temp[data[2]] += 1
temp[10] += 1
temp[10] = temp[10] + 1 = 0 + 1 = 1
temp[data[3]] += 1
temp[23] += 1
temp[23] = temp[23] + 1 = 1 + 1 = 2
temp[data[4]] += 1
temp[5] += 1
temp[5] = temp[5] + 1 = 0 + 1 = 1
So in this case, you’ll notice that value 23 occurs two times in the data array, while the other ones occur only one time. This way, you can count the occurrences in O(n), that is, with just one traversal of the array.
1
solved Understanding some code from my homework