This is an extension of the question asked in The Median Minimizes the Sum of Absolute Deviations (The $ {L}_{1} $ Norm) . Except with the extra constraint that $x \in S$.
The solutions provided there doesn't seem to work when the cardinality of the set is even (if it is odd, the solution is the median again). I'm particularly interested in the optimization approach (the one in Royi's answer).
With the above constraint, we now have a constraint optimization problem rather than an unconstrained problem:
$$ \arg \min_x \sum_{i=1}^n |s_i - x| \\ \text{subject to: } x \in S $$
I'm pretty certain that the solution to this problem, when the size of the set is even, is that $x$ can either be the $floor(n/2)$-th or $ceiling(n/2)$-th element of the SORTED set, where $n$ is the size of the size. For example, say $S = \{1,2,3,4,5,6,7,8\}$, so $n=8$. I believe $x$ can be 4 or 5 in this case.
Using Royi's approach, I am not sure how to account for the constraint. Would lagrange multipliers work for a constraint like $x \in S$?