input: from typing import List
  
  def gini(x: List[float]) -> float:
      """Calculate Gini Coefficient of a given dataset.
      >>> gini([2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,21])
      0.605
      """
output:     n = len(x)
      if n == 0:
          return 0.0
      x_sorted = sorted(x)
      cum_sum = [0] * n
      for i in range(n):
          cum_sum[i] = cum_sum[i - 1] + x_sorted[i]
      total_sum = cum_sum[-1]
      gini_sum = 0
      for i in range(n):
          gini_sum += (i + 1) * x_sorted[i]
      gini_coefficient = (2 * gini_sum) / (n * total_sum) - (n + 1) / n
      return round(gini_coefficient, 3)