Lets say we have a set of numbers $\{ 5, 7, 1, 2, 5, 100 \}$. I want to find a number $x$ such that the sum of distances of every number from the set to $x$ is minimal.
My first thought was that $x$ is the average of all elements of the set: $\frac{5+7+1+2+5+100}{6}$, but it is not true, it fails the above example.
Any help or hint will be appriciated, thanks.
You are looking to minimize $$\sum_{y \in A} |y - x|$$ with respect to $x$ where $A$ is your set.
It can be proved that any median minimizes this problem. In your case, the only median is $5$, so that's the result.