Let the distance from one location to other be $x$. Then, the total distance travelled by body is $2x.$ For the first part, distance $=x,$ velocity $=v_1$ and time taken $=t_1.$
Then, \[t_1=\frac{x}{v_1}\]
For the second part, distance $=x,$ velocity $=v_2$ and time taken $=t_2.$
Then, \[t_2=\frac{x}{v_2}\]
Now, the total time is given by \[t=t_1+t_2=\frac{x}{v_1}+\frac{x}{v_2}\] \[=x\left(\frac{1}{v_1}+\frac{1}{v_2}\right)=x\left(\frac{v_1+v_2}{v_1v_2}\right)\]
Therefore, average speed is given by, \[v_{\text{av}}=\frac{2x}{t}=\frac{2x}{x\left(\frac{v_1+v_2}{v_1v_2}\right)}=\frac{2v_1v_2}{v_1+v_2}\]
[Read: Motion in a Straight Line]