This is a simple matter of summing the contents and then dividing by the number of items. It is an O(n) operation.
2
Anonymous
Sep 2, 2018
Maybe he was asked a better way to do it, because sum(array) can overflow. In that case, what you can do is a_m = a_(m-1) + (a_i - a_(m-1))/n, where a_m is the new average and a_i the new element to add. The main idea is to calculate a running average that is preventing overflow, but it won't escape it's fate if the actual average is very big.