The int[] nums contains source data.
So let's imagine the following.
We have the following data
int nums[] = { 1, 2, 1 };
At the beginning we defined the variable goodPairs to the 0 and we initialized the new array int[] counts. Every 101 positions of this array has the 0 value. So it might looks like:
int counts[] = { 0, 0, 0, 0, 0, 0, ......... n} //n is the 101-th position
In for loop we are looping over the nums array and doing this:
goodPairs = goodPairs + counts[1]; //at the first time goodPairs = 0, counts[1] is 0
counts[1] = counts[1] + 1; //then we increment the value at the `num` position (in this case is the `num` 1 - the first value from our array `nums[]`)
The result of goodPairs is after first loop 0, and the count at the first position is 1
int counts[] = { 0, 1, 0, 0, 0, 0, ......... n} //n is the 101-th position
Second looping:
goodPairs = goodPairs + counts[1]; //at the second time goodPairs = 0, counts[2] is 0
counts[2] = counts[2] + 1; //then we increment the value at the `num` position (in this case is the `num` 2 - the third value from our array `nums[]`)
The result of goodPairs is after second loop still 0, and the count at the first position is 1 and at the second too
int counts[] = { 0, 1, 1, 0, 0, 0, ......... n} //n is the 101-th position
The last looping:
goodPairs = goodPairs + counts[1]; //at the second time goodPairs = 0, counts[1] is now 1
counts[1] = counts[1] + 1; //then we increment the value at the `num` position (in this case is the `num` 1 - the second value from our array `nums[]`)
So the result will be
goodPairs = 1;
int counts[] = { 0 (quantity of number 0), 2 (quantity of number 1), 1 (quantity of number 2), 0 (quantity of number 3), 0 (quantity of number 3), 0 (quantity of number 4), ......... n} //n is the 101-th position
Now you know that there are two numbers 1 and one number 2 in your array nums[] - from counts[] array