Get its oxidation number of that element and multiply by the atoms present in that compound.
Example: H2SO4: H has an oxidation of +1 (here H is a cation that is why +1), multiply it by 2 that’s 2. We don’t know what the oxidation number is for S since we have +/- 2, +/- 4 and +/- 6 so we proceed to O which we know have an oxidation number of -2. So that’s, -2 multiplied by 4 that’s -8. In a neutral compound, the sum of all the electrical charges must be equal to zero and looking to our example, we already have 2 and -8 so that is -6, therefore since S must have a -6 charge, dividing it by the number of atoms which is 1, we get -6.
Therefore, H has +1, S has +6 and O has -2.