The average is below, so if you know the previous average:
At = (A0 * N + E) / (N + 1) At is the average after addition of the new element A0 is the average before addition of the new element N is the number of element before addition of the new element E is the new element value
Its simple brother works if you keep the tab of the sum of elements:
At = (T + E) / (N + 1) T is the total of all elements A0 is the average before addition of the new element N is the number of element before addition of the new element E is the new element value
When the value is deleted, you can do a similar thing:
At = (A0 * N - E) / (N - 1)
And when the value is updated:
At = (A0 * N - E0 + E1) / (N) E0 is value before updating, E1 is value after updating.
source share