input: Write a function to calculate the Gini coefficient of a given dataset.
  
  def gini(x):
      """Calculate Gini Coefficient of a given dataset."""
      import numpy as np
      x = np.array(x)
      # calculate the frequency of each item in x
      x_value_counts = np.bincount(x) 
      total_count = len(x)
  
      # calculate the cumulative sum of x_value_counts
      x_cum_sum = np.cumsum(x_value_counts[1:])
  
      # calculate the Lorenz values
      lorenz = x_cum_sum / total_count
      lorenz = np.insert(lorenz, 0, 0) # add 0 to the beginning of array
  
      # calculate the Gini Coefficient
      width = -np.diff(lorenz)
      gini_coef = (1 / total_count) * np.sum(width * (lorenz[1:] + lorenz[:-1]))
  
      return gini_coef
  
  assert np.isclose(gini([2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,21]), 0.605)
  assert np.isclose(gini([1,1,1,1,1,1,1,1,1]), 0.0)
  assert np.isclose(gini([1,2,3,4,5,6,7,8,9,10]), 0.3)
  assert np.isclose(gini([]), 0.0)
  assert np.isclose(gini([21]), 0.0)
output: def gini(x):
      import numpy as np
      x = np.array(x)
      if len(x) == 0:
          return 0.0
      x_value_counts = np.bincount(x)
      total_count = len(x)
      x_cum_sum = np.cumsum(x_value_counts[1:])
      lorenz = x_cum_sum / total_count
      lorenz = np.insert(lorenz, 0, 0)
      width = -np.diff(lorenz)
      gini_coef = (1 / total_count) * np.sum(width * (lorenz[1:] + lorenz[:-1]))
      return gini_coef