def get_distances(sequence):
    """
    Get distance between two occurrences of each unique element in sequence

    Returns 0 if it only occurs once, returns the distance between the last two occurrences if element occurs more
    than twice 0 if only occurs once

    :param sequence: list
    :return: dictionary with (element, distance) as key, value pairs
    """
    distances = dict((s, {}) for s in set(sequence))
    for i in range(len(sequence)):
        distances[sequence[i]]["distance"] = i - distances[sequence[i]].get("last index", i)
        distances[sequence[i]]["last index"] = i
    return {key: value["distance"] for (key, value) in distances.items()}