Sum of Distances
Group equal values, then use running sums of their positions to get each index’s total distance to the others in one pass per group.
Group equal values, then use running sums of their positions to get each index’s total distance to the others in one pass per group.