[Question]
  [
# Challenge
You are given an array \$a\$ of integers. With a ***move*** you can **increase or decrease** an element of the array **by 1**. Your task is to ***equalize*** the array, that is make all the elements of the array equal by performing some *moves*. But that's not enough! You also want to make **as few *moves* as possible**.
## Input
* A non-empty **array** \$a\$ of integers
* Optionally, the **length** of \$a\$.
## Output
* The **minimum number of *moves*** needed to *equalize* the array \$a\$.
# Rules
* **Standard rules** for [valid submissions](https://codegolf.meta.stackexchange.com/q/2419/82619), [I/O](https://codegolf.meta.stackexchange.com/q/2447/82619), [loopholes](https://codegolf.meta.stackexchange.com/q/1061/82619) apply.
* This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so **shortest solution** (in bytes) wins. As usual, don't let ridiculously short solutions in golfy languages discourage you from posting a longer answer in your language of choice.
* This is not a rule, but your answer will be better received if it includes a link to test the solution and an explanation of how it works.
# Examples
```
Input                       --> Output
[10]                        --> 0
[-1, 0, 1]                  --> 2
[4, 7]                      --> 3
[6, 2, 3, 8]                --> 9
[5, 8, 12, 3, 2, 8, 4, 5]   --> 19
[1,10,100]                  --> 99
```
  
[Answer]
# [Wolfram Language (Mathematica)](https://www.wolfram.com/wolframscript/), 19 bytes
```
Tr@Abs[#-Median@#]&
```
[Try it online!](https://tio.run/##y00syUjNTSzJTE78n2Yb8z@kyMExqThaWdc3NSUzMc9BOVbtf0BRZl6JQ5pDtaFBLReco2uoo2Cgo2CIJGSio2COxDXTUTDSUTDWUbBAEjQFcoG6IBJGYA5Qm2ntfwA "Wolfram Language (Mathematica) – Try It Online")
For 1D integer array, `Tr` works the same way as `Total`.
## How?
Simple application of triangle inequality.
... 
I originally intended to write the proof here, but then decide to look up <https://math.stackexchange.com> instead and found [The Median Minimizes the Sum of Absolute Deviations (The \$ {L}\_{1} \$ Norm)](https://math.stackexchange.com/questions/113270/the-median-minimizes-the-sum-of-absolute-deviations-the-l-1-norm).
By knowing the name of the operator, this is an alternative 19-byte solution:
```
Norm[#-Median@#,1]&
```
[Answer]
# [JavaScript (Node.js)](https://nodejs.org), ~~50~~ 48 bytes
Saved 2 bytes thanks to Arnauld
```
a=>a.sort((x,y)=>x-y,r=0).map(n=>r+=a.pop()-n)|r
```
[Try it online!](https://tio.run/##VczPDoIwDAbwu0@xcOpiRzYQ/8SUFzEeFh0JJsLs0MDT4wJcduvX39e@7M@GB7d@UF3/dHNDs6UamDTaPPQ8AIw4SapHNcn8bT10VPOebO57D1J1ElnONgTHA7H7fFt2kK05k9fdOkEDN6PvUhCR0MlaGRQahYm4aJHoAcVpOysTOKIoUJQozhtfEq4ixKdrpVhCfFVtXRPL8x8 "JavaScript (Node.js) – Try It Online")
Sort the array ascending then sum:
```
  a[last]   -a[0] // moves to equalise this pair
+ a[last-1] -a[1] // + moves to equalise this pair
+ ...etc
```
[Answer]
# [Perl 6](https://github.com/nxadm/rakudo-pkg), ~~29~~ 28 bytes
*-1 byte thanks to nwellnhof*
```
{sum (.sort[*/2]X-$_)>>.abs}
```
[Try it online!](https://tio.run/##K0gtyjH7n1upoJamYPu/urg0V0FDrzi/qCRaS98oNkJXJV7Tzk4vMam49r81V3FipUKaRrShQawmnKNrqKNgoKNgiCRkoqNgjsQ101Ew0lEw1lGwQBI0BXKBuiASRmAOUJtprOZ/AA "Perl 6 – Try It Online")
### Explanation
```
{                          }  # Anonymous code block
      .sort[*/2]              # Get the median of the input array
                X-$_          # Subtract all elements from the median
     (              )>>.abs   # Get the absolute of each value
 sum                          # Sum the values
```
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 4 bytes
```
ÅmαO
```
[Try it online!](https://tio.run/##yy9OTMpM/W/q5vL/cGvuuY3@/3X@RxsaxHJF6xrqKBjoKBgCmSY6CuZAykxHwUhHwVhHwQLIMQVSQFmIgBGYA1RmGgsA "05AB1E – Try It Online")
**Explanation**
```
Åm     # push median of input
  α    # absolute difference with each in input
   O   # sum
```
[Answer]
# [Japt](https://github.com/ETHproductions/japt) [`-g`](https://codegolf.meta.stackexchange.com/a/14339/), ~~7~~ 6 [bytes](https://en.wikipedia.org/wiki/ISO/IEC_8859-1)
```
£xaXÃÍ
```
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&flags=LWc&code=o3hhWMPN&input=WzUsIDgsIDEyLCAzLCAyLCA4LCA0LCA1XQ)
```
£xaXÃÍ     :Implicit input of array U
£          :Map each X
 x         :  Reduce by U by addition after applying
  aX       :    Absolute difference with X
    Ã      :End map
     Í     :Sort
           :Implicit output of first element
```
[Answer]
# JavaScript (ES6), ~~60~~ ~~56~~ 55 bytes
*Saved 1 byte thanks to @Shaggy*
```
a=>a.map(r=k=>r=a.map(n=>m+=n>k?n-k:k-n,m=0)|m>r?r:m)|r
```
[Try it online!](https://tio.run/##fc5BDoIwEAXQvaeYJY0ttCAqJC0HMV00CEZLW1KMK@5eiSxMGnF2P/kvfx7qpabW38cnse7ahZ4HxYVKjRoTzzUXnq/BcmH23ArdWKJrTSw2nKLZCN/42qDZh9bZyQ1dOrhb0icXRiX8OoQgy4AQAXQXEcIwUAxMbpI8JgcMJ/l3pYjJEUOOocBwlhukikm5lJe/VpZ/wjJcyi9hVXgD "JavaScript (Node.js) – Try It Online")
### How?
Unless there's some trick that I'm missing, computing the median in JS turns out to be longer. Probably around **65 bytes** because of the required callback for `sort()` to circumvent the default lexicographical sort and the rather lengthy `Math.abs()`:
```
a=>a.sort((a,b)=>b-a).map(n=>s+=Math.abs(n-a[a.length>>1]),s=0)|s
```
Instead of that, we try all values in the original array as the *equalizing* value.
[Answer]
# [Haskell](https://www.haskell.org/), 34 bytes
```
f l=minimum[sum$abs.(m-)<$>l|m<-l]
```
[Try it online!](https://tio.run/##JU3LCsIwELz7FXPIQWEjbbXqIfVHQg4RFIO7oZjm1n@Piz3Nk5l3LJ8nc2sv8CQpJ6niSxUTH@W4F3tw5s6rOMuhpTzXpWCC930XyNue0BF6pWfCVeFCGAgnwk3FqKDpZgx/obUxhJ3ElHVm/qa8wMDr98rObvuh/QA "Haskell – Try It Online")
Finds the total distance of all elements to the median, testing each element in the list as the potential median and taking the smallest result.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 4 bytes
```
ạÆṁS
```
[Try it online!](https://tio.run/##y0rNyan8///hroWH2x7ubAz@f7j9UdMa9///o6MNDWJ1uKJ1DXUUDHQUDEFsEx0FcxBtpqNgpKNgrKNgAeKZAmmgAoiIEZgDVGgaGwsA "Jelly – Try It Online")
### How it works
```
ạÆṁS – Full program. Takes an array A of integers as input from argument 1.
 Æṁ  – Median. For odd-length A, middle element of S. For even-length A, the
       arithmetic mean of the two middle elements of S. Where S = A sorted.
ạ    – Absolute difference of each element with the median.
   S – Sum.
```
[Answer]
# [Python 2](https://docs.python.org/2/), 46 bytes
```
lambda l,n:sum(l[-~n/2:l.sort()])-sum(l[:n/2])
```
[Try it online!](https://tio.run/##TcxBDoIwFATQvaf4yzaZKi2gpMST4F9gDNGkFAJ14Yar10YS0@W8ycz8Cc/Jmzhcb9H14/3Rk4O363sUrlObPxnrjuu0BCFZqp1tUpZxXl4@0CA6XTC0bA9/UBpUgDSjzLkCXRgmpzPIgEpQw6jyok6UHvbS/EKa14xGxi8 "Python 2 – Try It Online")
Takes the list length `n` as an argument. Computes the top-half sum minus the bottom-half sum by slicing the sorted list into the first `n/2` and last `n/2` elements.
The expression `l[-~n/2:l.sort()]` is equivalent to computing `l.sort()`, which modifies the list in place, then doing `l[-~n/2:None]`, where the list slicing ignores upper bound of `None` that `l.sort()` produced. It might seem like the list was sorted too late to be sliced right, but Python seems to evaluate the slice arguments before "locking in" the list to be sliced.
---
**[Python 2](https://docs.python.org/2/), 47 bytes**
```
lambda l,n:sum(abs(x-sorted(l)[n/2])for x in l)
```
[Try it online!](https://tio.run/##TczRCsIgGAXg@57iv1Q4o@m2GkVPsrzYWNLA6VCD9fQmBeHl@Q7nbO/4dFYmfbsnM67TPJKBvYTXysYpsL0KzsfHzAwf7FEqrp2nnRZLhqfNLzaSZoOoFQS/Hv5QCVANEgpNyS3orCBLOoEkqAH1Cm1ZdJnyw6@U35DnnULP0wc "Python 2 – Try It Online")
The boring method of summing the distance of each value from the median. Takes the length `n` as an argument.
---
**[Python](https://docs.python.org/2/), 51 bytes**
```
f=lambda l:l>l[l.sort():1]and l[-1]-l[0]+f(l[1:-1])
```
[Try it online!](https://tio.run/##VY1BCsIwEEX3nmKWCU4kE62ViF5kyCJSgsKYlpqNp49BQcjyvc/jL@9yn7OrNV0kPm9TBPFyFZbda16L0p5CzBMIGwpG2IZtUsLkG@q6rI9cICkmG/R580dDCBaBOnlAGDtxRHAIe4RTp4cmWvub3BdaOrS7Dw "Python 2 – Try It Online")
Sorts the list in place, then repeatedly adds the last (highest remaining) entry minus the first (lowest remaining) entry, and recurses on the list without these elements until only 0 or 1 remain. Usings `pop`'s gets the same length: `l.pop()-l.pop(0)+f(l)`.
The `l.sort()` is stuck in a place where the `None` it returns has no effect. The slice `l[None:1]` is the same as `l[:1]` because `None`s in slices are ignored.
---
**[Python](https://docs.python.org/2/), 54 bytes**
```
lambda l:sum(l.pop()-l.pop(0)for _ in l[1:l.sort():2])
```
[Try it online!](https://tio.run/##VY3RCsIgGIXve4pzqfBvqLUKY0@yRixCGjgVZxc9vZMGgVeH7zscTvimt3cqm/6e7bQ8XxOsXj8Ls23wgfFmT8GNj3hgdrCD1LZdfUyMazXyHOLsEgwbpBj57fDHRhIEQVbyRLhU4kxQhCPhWumuiLLdK/WDMu3K3QY "Python 2 – Try It Online")
A cute list comprehension that ignores the argument iterated over and modifies the list in place by repeatedly popping the first and last elements. We ensure that the list comprehension is done `len(l)//2` times by iterating over every other element of `l` skipping the first, done with `l[1::2]`. The `l.sort()` producing `None` can be stuck in the unused slice end argument.
[Answer]
# APL(Dyalog), 12 bytes
```
{⌊/+/|⍵∘.-⍵}
```
Brute forces by testing each number as the equalizer. Not sure if tacit is shorter, but I can't figure it out.
[TIO](https://tio.run/##SyzI0U2pTMzJT9dNzkksLs5M/g8E1Y96uvS19Wse9W591DFDTxdI1xoqGCkYc2GTMQPJKFhglTNVsFAwBEkbARkmCqYA)
[Answer]
# TI-Basic, ~~18~~ 6 bytes
```
sum(abs(Ans-median(Ans
```
-12 bytes from [Misha Lavrov](https://codegolf.stackexchange.com/questions/173038/equalize-the-array/173052?noredirect=1#comment417510_173052) (I haven't used TI-Basic in a while and I forgot it's lists can do that)
TI-Basic is a [tokenized language](http://tibasicdev.wikidot.com/one-byte-tokens). All tokens used in this answer are one byte.
Takes input as `{1,2,3,4}:prgmNAME`
Basically the same idea as most other answers: subtract through by the median, then take the sum.
Explanation:
```
sum(abs(Ans-median(Ans
sum(                    # 1 byte, Add up:
    abs(                # 1 byte, the absolute values of
        Ans-median(Ans  # 4 bytes, the differences between each element and the list's median
```
[Answer]
# [Röda](https://github.com/fergusq/roda), 33 bytes
```
{|a|a|abs _-[sort(a)][#a//2]|sum}
```
[Try it online!](https://tio.run/##PYzNCoMwEITveYrFXhRW/KltpWDvfQYrktZYhJqEbDypz26NgjOXYfhmjGr40vNOwshgVQv3Al7LOHHnN0EdlqSM9XlQlSceRWk10dDPywYrA1aQrT@cBDRqe3DqpB6seypJ/zrrHxACCV146AUwgeaGxFNa8RXGr4PqmHNJbtz6209w9Np00u4lggfhAzx08E40Sgo2L0nMwgRjTFiGN3bFFM@YswvmmLiYriHDyx8 "Röda – Try It Online")
Explanation:
```
{|a| /* Anonymous function with parameter a */
  a|         /* Push items in a to the stream */
             /* For each _ in the stream: */
  abs        /*   Abstract value of */\
  _-         /*   the value from stream minus */\
  [sort(a)][ /*     the value in the sorted version of a at index */
    #a//2    /*       length of a / 2 (the median) */
  ]|
  sum        /* Sum of all values in the stream */
}
```
[Answer]
# C (gcc), ~~100~~ 93 bytes
```
e(q,u,a,l,i,z)int*q;{i=1<<31-1;for(a=u;a--;i=z<i?z:i)for(l=z=0;l<u;)z+=abs(q[l++]-q[a]);q=i;}
```
Brute-force solution, tries to equalize with each element. Try it online [here](https://tio.run/##VZBdS8MwFIav7a84FITEneLSD3VLgwgqiII/YO4idtkM1G79mGhHf3tNm03MXc9znvdtkizYZFnfK1LiHiXmqLGlumguSn7QgqVpxALG19uKSLHnMgi4Fm2qb9u5pgPNRSumPE/3nLYTId9rUi7yyWQZlAu5pLwUmne96QNZVfKHLZYg4MCmHVoQWhAwhCkCO@HI4hjh@oRii64QQoQI4ea0SOwiMcg02GU4DiaedNzzvrZ6BY2qGzJczKYQhkPlqtg0H/Zbfe9U1qgVhYN3tqsMWhP/vH4rfARFjiEboCDEnw@34L8@@zAH//Hu6eXh3qfc6zxv6PyUuiDD78fS8Qj2HRBq3art@jjSy@NoQtQ8hWn4Z4euHbp26MqRK0euHLly7MqxK89cOXHlxJXZbLx2/ws).
Thanks to [ceilingcat](https://codegolf.stackexchange.com/users/52904/ceilingcat) for golfing 7 bytes.
Ungolfed:
```
e(q, u, a, l, i, z) int *q; { // function taking an array of int and its length; returns an int (extra parameters are variables and don't have to be passed when calling e())
    i = 1 << 31 - 1; // construt the maximum value of a signed 4-byte integer
    for(a = u; a--; i = z < i ? z : i) // loop through the array, testing each element as the equalizer; if the number of moves is smaller than the current minimum, set it as the new minimum
        for(l = z = 0; l < u; ) // loop through the array ...
            z += abs(q[l++] - q[a]); // ... and sum the number of moves it takes to equalize each element
    q = i; // return the minimum number of moves
}
```
[Answer]
# [R](https://www.r-project.org/), 29 bytes
```
sum(abs(median(a<-scan())-a))
```
[Try it online!](https://tio.run/##K/r/v7g0VyMxqVgjNzUlMzFPI9FGtzgZSGtq6iZqav43VbBQMDRSMFYwAjJMFEz/AwA "R – Try It Online")
[Answer]
# PHP, 69 bytes
```
function($a,$c){for(sort($a);$c-->$d;)$s+=$a[$c]-$a[+$d++];return$s;}
```
anonymous function. [Try it online](http://sandbox.onlinephpfunctions.com/code/5c2461fcd9f794c6a360dd11a5519eb60b5d7050).
[Answer]
# Java 8, 116 bytes
With `java.util.List<Integer>` as parameter, which has a short sort-builtin, but is longer to retrieve a value at a given index:
```
L->{L.sort(null);int r=0,l=L.size(),k=l;for(;k-->0;)r+=Math.abs(L.get(k)-(L.get(l/2+~l%2)+L.get(l/2)>>1));return r;}
```
[Try it online.](https://tio.run/##dVBda8IwFH3frwjCIKFp1na6ybIW9jiovvg49hBrdLExleTW4cT99S7tihTHXi73fHA592zFQYTbVdkUWjiHZkKZ0w1CyoC0a1FING9hR6ACb72b1aA0y5WD51fv2kiboZxw7zrf@OFAgCrQHBmUoiYPs1POXGUBm1prwts7No2oTj2tviQmtEw1X1cW8zIMs4gTG6QzAR9MLB3O2UYCLknYb/ouCb71bUKCCyZZFhPCrYTaGmT5ueFtjn291D5HH@dQqRXa@efwAqwym7d3JMjvZyAd4DjqPuhRGNOIxkNmTB@H8IEm9J5Oh9SETmncsolfxnQy1GIa@3tRdMXR8Z/aupyd3nfLGLsk/bd937SRnwP9xVpx7EwZvmIdE65VsCB9nMXRgdyxqga2992ANjgPRk9oFBhW4Jz0Ic/NDw)
With `int[]` (integer-array) as parameter, which has a long sort-builtin, but is shorter to retrieve a value at a given index:
```
a->{java.util.Arrays.sort(a);int r=0,l=a.length,k=l;for(;k-->0;)r+=Math.abs(a[k]-(a[l/2+~l%2]+a[l/2]>>1));return r;}
```
[Try it online.](https://tio.run/##ZVDBisIwFLz7FUFYSGmabbp1Vza0sB@gF4/SQ6xV28ZUkldBxP317muVpejlMTOPvMxMpc4qqLZ1l2vlHFmo0lwnhJQGCrtTeUGWPR0EklOc64woT6J2m@BwoKDMyZIYkpBOBem1woO8hVLzH2vVxXHXWKD4pL9gk5DpRHFdmD0cWJ1ouWsslXUQpKH0rJ8sFBy42jiq1nUW4NTvkf@r36LMH3CWpsLzpC2gtYZYeetk7@PUbjT6eNg5N@WWHDEKXYEtzX7wfM8BhQMqwiHBgwWChUyMlZh9jekni9gHm4@lGZsz0asRgpjNxjvBBN4LwyeNxS@1DT6HPZbDOf93ubo4KI68aYGfMABoQ196heYeDrv1p99k6hueI358cuv@AA)
**Code explanation (of the array answer):**
```
a->{                               // Method with integer-array as parameter and integer return-type
  java.util.Arrays.sort(a);        //  Sort the input-array
  int r=0,                         //  Result-sum, starting at 0
      l=a.length,                  //  The length of the input-array
  k=l;for(;k-->0;)                 //  Loop `k` in the range (length, 0]:
    r+=                            //   Increase the result-sum by:
       Math.abs(a[k]-              //    The absolute difference of the `k`'th value of the array,
        (a[l/2+~l%2]+a[l/2]>>1));  //    and the median of the array
  return r;}                       //  And then return this result-sum
```
**General explanation:**
1. Sorts the input-list
2. Calculate the \$median\$
3. Calculate the absolute difference of each value with this \$median\$
4. Sum those
The median of the array \$a\$ is calculated by:  
1) Taking the sorted values at the (0-based) indices \$i\$ and \$j\$, which are calculated like this (using the `l/2+~l%2` instead of `l/2-(l+1)%2` trick to save bytes):  
\$j=\left\lfloor\frac{1/2}l\right\rfloor\$  
\$i=\left\lfloor\frac{1/2}l\right\rfloor - ((l+1) \bmod 2)\$  
Note that \$i=j\$ for odd-sized arrays.
2) And then adding these together, and integer-dividing them by 2 (using the `i+j>>1` instead of `(i+j)/2` trick to save bytes):  
\$median = \left\lfloor\frac{a[i]+a[j]}{2}\right\rfloor\$
After which we can use this \$median\$ to calculate the absolute difference with the current item, which we can use to calculate the total sum:  
\$\sum\limits\_{k=0}^{l} = \left\lvert a[k] - median\right\rvert\$  
with \$l\$ being the length of the array \$a\$, and \$k\$ being the (0-based) index.
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E/wiki/Commands), 4 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
δαOß
```
I know there is already [an existing 4-bytes 05AB1E answer](https://codegolf.stackexchange.com/a/173044/52210) using the median builtin `Åm`, but I decided to post this alternative 4-byter without this builtin.
[Try it online](https://tio.run/##yy9OTMpM/f//3JZzG/0Pz///P9pQx9AAiAxiAQ) or [verify all test cases](https://tio.run/##yy9OTMpM/V9Waa@k8KhtkoKSfaXL/3Nbzm30Pzz/v87/6GhDg1gdhWhdQx0DHUMQy0THHESZ6RjpGOtYgJimOhY6hiCeEZBhomMKEjPUMQSqNzCIjQUA).
**Explanation:**
```
δ     # Apply the following command double-vectorized on the given two arguments,
      # which will use the input-list twice implicitly, since the stack is empty:
 α    #  Absolute difference
      #  i.e. [1,10,100] → [[0,9,99],[9,0,90],[99,90,0]]
  O   # Sum each of these inner lists
      #  → [108,99,189]
   ß  # Pop and push the minimum of this list
      #  → 99
      # (after which this is output implicitly as result)
```
[Answer]
# [Attache](https://github.com/ConorOBrien-Foxx/Attache), 18 bytes
```
Sum##Abs@`-#Median
```
[Try it online!](https://tio.run/##SywpSUzOSP2fpmBl@z@4NFdZ2TGp2CFBV9k3NSUzMe@/X2pqSnG0SkFBcnosV0BRZl5JSGpxiXNicWpxdJqOQjRXtKFBrA5XtK6hjoKBjoIhiG2io2AOos10FIx0FIx1FCxAPFMgDVQAETECc4AKTWO5YmP/AwA "Attache – Try It Online")
## Explanation
```
Sum##Abs@`-#Median
            Median    take the median of the input list
     Abs@`-#          absolute difference with the original list
Sum##                 sum of all elements
```
[Answer]
# [J](http://jsoftware.com/), 15 bytes
```
[:<./1#.|@-/~"{
```
Essentially the same as Shaggy's Japt solution.
[Try it online!](https://tio.run/##y/qfVmyrp2CgYKVg8D/aykZP31BZr8ZBV79Oqfq/JpeSnoJ6mq2euoKOQq2VQloxF1dqcka@QpqCiYI5jGmoYKRgDOOYgTgKFjCuqYKFgiFIxAjIMFEw/Q8A "J – Try It Online")
## How it works?
`|@-/~"{` - creates a table `/~` of absolute differences `|@-` of each number to all others `"{`
```
   |@-/~"{ 6 2 3 8
0 4 3 2
4 0 1 6
3 1 0 5
2 6 5 0
```
`1#.` sums each row
```
   1#.|@-/~"{ 6 2 3 8
9 11 9 13
```
`[:<./` finds the smallest item (reduce by minimum)
```
   ([:<./1#.|@-/~"{) 6 2 3 8
9
```
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), ~~16~~ 11 bytes
```
Ｉ⌊ＥθΣＥθ↔⁻ιλ
```
[Try it online!](https://tio.run/##S85ILErOT8z5/z@gKDOvRMM5sbhEwzczLzO3NFfDN7FAo1BHIRjBdEwqzs8pLUkFKSkt1sjUUcjRhALr//@jo011FCx0FAyNdBSMdRSMwBwTHQXT2Nj/umU5AA "Charcoal – Try It Online") Link is to verbose version of code. Edit: Saved 5 bytes thanks to @Arnauld. Explanation:
```
  Ｅθ        Map over input array
     Ｅθ     Map over input array
         ι  Outer value
          λ Inner value
        ⁻   Difference
       ↔    Absolute value
    Σ       Sum
 ⌊          Minimum
Ｉ           Cast to string
            Implicitly print
```
[Answer]
## Visual C#, 138 bytes
```
int s=0;foreach(string i in a)s+=int.Parse(i);int x=s/a.Length;int o=0;foreach(string i in a)o+=Math.Abs(int.Parse(i)-x);Console.Write(o);
```
ungolfed:
```
int s = 0;                    // Takes a string array of arguments a as input
foreach (string i in a)       
     s += int.Parse(i);       // s as sum of the array elements
int x = s / a.Length;         // calculating the target value of all elements
int o = 0;                    // o as minimum number of moves
foreach (string i in a)
     o += Math.Abs(int.Parse(i) - x);    // summing up the moves to the target value
Console.Write(o);
```
[Try it online!](https://tio.run/##dYyxCsIwFEV3vyJjQ2nVakEIHcTVQsHBQRyeMbYP2gTyolRKv/1ZxcHF7XLPuVdToknzndDW4vCkYDo10y0Qicq72kM3UICAWjwcXkUJaCMKfrJPZwFyYLRBULFQN@cN6OYLBQq0E6e4mIS0Ak8mQqnedl/QHNK9sXVoPoX7O3dxUUJo0u2Fot@fpJdq5yy51qRHj8FETioeR@acN7zMeMXZFNacvwA)
[Answer]
# PHP, 78 bytes
Sorts the array, then loop through a copy, popping elements off the original and summing the absolute difference, that needs to be halved for the return.
```
function m($n){sort($n);foreach($n as$i)$r+=abs(array_pop($n)-$i);return$r/2;}
var_dump(
    m([10]),
    m([-1, 0, 1]),
    m([4, 7]),
    m([6, 2, 3, 8]),
    m([5, 8, 12, 3, 2, 8, 4, 5]),
    m([1,10,100])
);
```
Output:
```
int(0)
int(2)
int(3)
int(9)
int(19)
int(99)
```
[Answer]
# [Ruby](https://www.ruby-lang.org/), 38 bytes
```
->a{a.map{|x|a.sum{|y|(y-x).abs}}.min}
```
[Try it online!](https://tio.run/##LctBDsIgFATQNZ7iJ2w0@RCgttqFXqRh8bto4gLT2DQpAc6OCC7nzcxnn31eHlk8KZB0tIZ4RJLb7kL08ezFcZE0bylJ93qnvMIyTVpZC4wxDupUQWgEhaCtrWqaXhFuf@maDAgGoUO4/5zD2LgvUN6tMjWUb182HPSYvw "Ruby – Try It Online")
[Answer]
# [C++ (gcc)](https://gcc.gnu.org/), 94 bytes
```
#import<regex>
int f(int c,int*a){int s=0,d=0;for(std::sort(a,a+c);c-->d;)s+=a[c]-a[d++];c=s;}
```
Port of the [PHP solution](https://codegolf.stackexchange.com/a/173068/89298). Kudos to [Titus](https://codegolf.stackexchange.com/users/55735/titus)!
Input size of the array and a non-`const` mutable array. Output the minimum number of moves required to equalize the array.
*-4 bytes thanks to [ceilingcat](https://codegolf.stackexchange.com/users/52904/ceilingcat)!*
[Try it online!](https://tio.run/##VVDRasMgFH33Ky4MVl1MSbp1GzHxR7o@iNoiLDFEI2Ml354ZA13jw/Wec8/x4JV9n1@lnOcn0/Z28PWgr/qHI9N5uOClShrriyC3BbimoKop2MUO2HlVVS56sKAik4TJPOeKEZc14iTPuTipLDsz2Tg2xec7@T0qDbWxzg9atBz9c0FLbweOUmwrTIeDNYqgG4J4UtAqqR/7KOYcTNKsytRBWcBEH3BeUigolFv2jcLHlnmncKDwSuFzyx8jE@3r7JBANB@3ophRLiHFPXxiKN3LrsToLTwHqMCQuyn9RdrRQ13HZYe9M78aEwphr4QXmJBlsPvqdgxN8x8 "C++ (gcc) – Try It Online")
[Answer]
# [V (vim)](https://github.com/DJMcMayhem/V), 84 bytes
```
:sort
Go<c-r>=(line('$')-1)/2
<esc>dd@"kk0vg_"ayGqq0C<c-r>=abs(<c-r>"-<c-r>a)
<esc>k@qq@q:%s/\n/+
$x0C<c-r>=<c-r>"
<esc>
```
[Try it online!](https://tio.run/##K/v/36o4v6iEyz3fJlm3yM5WIyczL1VDXUVdU9dQU9@Iyya1ONkuJcVBKTvboCw9Ximx0r2w0MAZojgxqVgDzFLSBVOJmhD12Q6FhQ6FVqrF@jF5@tpcKhUwDRDFEEX//5tyWXAZGnEZcxkBGSZcpv91ywA "V (vim) – Try It Online")
`<c-r>` is very helpful in finding the median and the whole calculation in general.
[Answer]
# [PowerShell](https://github.com/TryItOnline/TioSetup/wiki/Powershell), 52 bytes
```
($a=$args|sort)|%{$_-$a[--$i]}|%{$s-=$_*($_-lt0)}
$s
```
[Try it online!](https://tio.run/##PY/tCsIgFIb/exWHOAsNhdl3waD7iBij7AMGLV1UbF77Op1aIsj7PL7Kqa4P58PZlWWHR8ig6SQWGRb@FNpw9bVqkwZzg8XWGLzs4icGk2E@koTLOlVRYOiiEBspgJaWqf6cNlW/POZsrAYytqcTplMNi56smMw1UIHssueWxYwI1b9uzIHKs3@ZL1ltU9r0tYIWEmjYontWbl@7gwYazBcvmhPzr/Iu3MuawJDG37BlMUD5c8bd/g@odd8YiNi9AQ "PowerShell – Try It Online")
[Answer]
# [Perl 5](https://www.perl.org/) `-ap`, 39 bytes
```
map$\+=abs$_-(sort{$a-$b}@F)[@F/2],@F}{
```
[Try it online!](https://tio.run/##K0gtyjH9/z83sUAlRts2MalYJV5Xozi/qKRaJVFXJanWwU0z2sFN3yhWx8Gttvr/f1MdBQsdBUMjHQVjHQUjMMdER8H0X35BSWZ@XvF/3cQCAA "Perl 5 – Try It Online")
[Answer]
# [PowerShell](https://github.com/TryItOnline/TioSetup/wiki/Powershell), 107 bytes
```
$f={param($i)$m=($i|sort)[[int](($i.count-1)/2)];($j+=$i.foreach({([math]::abs($m-$_))})|measure -sum).Sum}
```
[Try it online!](https://tio.run/##DchNCoMwEEDhq3QxixlKLHZpySm6DKFMJRKLYyQ/dKGePWb1@N4W/i4m75alVpj0vnFkQZgJRLccKcRMxsxrttjcjaGsWfX0eJJ9Ifzuus0pRMejxx2NcPZ2GPibEETBh@ikQxynEt1NpSLUvYuctV4 "PowerShell – Try It Online")
# [PowerShell](https://github.com/TryItOnline/TioSetup/wiki/Powershell), 115 bytes
```
function f($i){$m=($i|sort)[[int](($i.Count-1)/2)];return($j+=$i.ForEach({([math]::Abs($m-$_))})|measure -Sum).Sum}
```
[Try it online!](https://tio.run/##DYqxCsIwFEV3vyJDhjykio4tHUR0EwTHUiTWtI00ibz3okjbb69Z7uFw7jt8DVJvhmGJZH0nbj9i44pVM2giccXQoXYjsWbbiE@wT3HR1itiTO@qFhrGpY2@YRu8aJW0MEpXJk4UkKGqrOdaJd8cQ/Sc7WC7h7pAwxG9kq91mdI54Ek3vRpV5TT3dZ4fHqSky@QdYIbJGU0Rjchu0cEmzbzM8/IH "PowerShell – Try It Online")
[Answer]
# [JavaScript (Node.js)](https://nodejs.org), 52 bytes
```
f=a=>(v=a.sort((x,y)=>x-y).pop()-a.shift())?v+f(a):0
```
[Try it online!](https://tio.run/##bYzBCsIwEAXvfkWOu7gJSbUqQuqHiIdQG20pTWhCab8@Br1Jjm9meINZTGjn3kc@uWeXktVGN7BoI4KbI8BKG@pm5RsK7zwgz@Ld2wiIt2VvweBVptZNwY2dGN0LLNyVfCDu/iBXxCQxVVBHYucCPhGriB2IXQqyzji//YLqO/JNncv0AQ "JavaScript (Node.js) – Try It Online")
[Answer]
**Kotlin Android, 200 bytes**
```
fun m(a:IntArray){var d=0;var s=0;var p=a.max()!!.times(a.size);var x =0;for(i in a.indices){x=a[i];d=0;s=0;while(d<a.size){if(x-a[d]<0)s=((x-a[d])*-1)+s;else s=((x-a[d]))+s;d++};if(p>s)p=s};print(p)}
```
[Try Online](https://rextester.com/live/SGDG37478)
]