The coins differ in value by 5¢, so swapping the numbers of them will change the value by 5¢ for each unit difference in the numbers of coins. Since
40¢ = 8 * 5¢
there must be 8 more dimes than nickels.
There are (22 +8)/2 = 15 dimes and 7 nickels.
_____
You could write some equations for this problem. Let n, d represent numbers of nickels and dimes.
.. n +d = 22
.. 10d +5n - (10n +5d) = 40 . . . . . . . cents
.. 5(d -n) = 40 . . . . . . . . . . . . . . . . . . reversing the coin count changes the total by 5 cents for each unit of difference (d -n), as stated above.
.. d -n = 8 . . . . . . . . . . . . . . . . . . . . . . divide the preceding equation by 5
Adding this last equation to the first gives
.. 2d = 22 +8
.. d = 30/2 = 15
.. n = 22 -15 = 7
There are 15 dimes and 7 nickels.