Array

extension Array
extension Array where Element: Hashable
  • An extension method that takes an IndexSet and uses it to mutate an array so that it only contains the values at the given indices.

    The use of IndexSet is predicated on the fact that it is a collection of increasing integers and can therefore be mapped onto the corresponding array element.

    There are two main task phases to the method:

    1. Ensure that the indices of the IndexSet are valid for the array by filtering the IndexSet so that it only contains indices with values less than the array count.
    2. Map each index to the corresponding array element mutating the original array with the return value.

    Declaration

    Swift

    mutating func arrayFromIndexes(_ indexes: IndexSet)

    Parameters

    indexes

    An IndexSet.

Available where Element: Hashable

  • An extension method that returns an array of unique duplicate elements contained in an array.

    There are three main task phases to the method:

    1. Create a dictionary using the init(grouping:by:) initialiser whose keys are the grouping returned by each element in the original array.
    2. Filter the values of each grouping for those that have a count greater than 1. These will be the array values that have duplicates.
    3. Create a new array from the filtered keys of the dictionary. This will contain the unique duplicates of the original array.

    Note

    Note The array element must conform to Hashable.

    Declaration

    Swift

    func uniqueDuplicates() -> Array

    Return Value

    An array that contains the unique duplicate elements contained in the original array.