It is necessary to check the array for a repeated name

Hello! I need to check the array for repetition of the name. If there are two identical names in the array, then it is necessary to additionally output the position in order to sort of separate the same names.

If you simply want to remove duplicates you could use Set(array)

If you want the indices of duplicate values then you could do the following:

let array = ["a", "b", "c", "a", "d", "e", "f"]

let dictionary = Dictionary(grouping: array.enumerated(), by: { $0.element }).mapValues { $ { $0.offset } }

let duplicates = dictionary.filter { $0.value.count > 1 }

print("duplicates = \(duplicates)")

It's best not to indulge what look like obvious homework questions.

@dude I would suggest trying to implement it yourself and then post your code, asking questions about aspects you weren't otherwise able to figure out through your own research.

1 Like