I have solved the above question but I found another method for it on a website but didn't understand.
Alternative method:
The sum of all the numbers formed by the digits a1, a2, a3,……….an, without repetition of the digits is given by:
(n-1)!(a1 + a2 + a3 + ………an) (10n -1)/9
Hence, the sum of the given numbers = 4!(1 + 3 + 5 + 7 + 9) × 11111 = 6666600.
Where did this formula come from?
How many numbers have a $9$ in the left-most digit position? This must be $4!$ because the other four digits can be in any order, and each order gives you one such number.
How many numbers have a $9$ in the second digit position? The answer to this is exactly the same.
The same for the third position, and so on.
So all those nines occur $4!$ times in the $10000$ position (which contributes $4!\times9\times10000$ to the sum), $4!$ times in the $1000$ position (which contributes $4!\times9\times1000$ to the sum), etc.
Adding those together, in total the nines contribute $4!\times9\times11111$ to the sum.
The same argument is valid for the other digits too, so the sevens contribute $4!\times7\times11111$, the fives $4!\times5\times11111$ etc.
Adding those together you get $4!\times(1+3+5+7+9)\times11111$.
If you want to generalise this to numbers with $n$ digits, you need to be able to write the number $111....111$ that has $n$ digits. These numbers are called repunits, and can be written as $(10^n-1)/9$ because $10^n-1$ is a number with $n$ nines.
So the correct general formula is $(n-1)!(a_1+...+a_n)(10^n-1)/9$.