Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

192
Visualizações
How can I process this array more efficiently

I have an array of names that looks like the following. ["John Doe", "Mary Blog"]

What I want to do is process this array and return an array with the initials like the following.

["JD", "MB"]

I have already done this but my code is ugly and inefficient (nested loops). It's probably fine for a small array like in my example.

How can I process my array more efficiently?

       let contacts = ["John Doe", "Mary Blog"]
       var initalsArray: [String] = []
       let names = contacts.map {$0}

            for name in names {
                var initals: String = ""
                let fullname = name.components(separatedBy: " ")
                for part in fullname {
                    if part.count > 0 {
                        initals += part[0]
                    }
                }
                initalsArray.append(initals)
            }
over 4 years ago · Santiago Trujillo
4 Respostas
Responde à pergunta

0

You must process all elements in the array.

For every element, you must detect the spaces and retrieve the next letter.

Then you must build up the output array with concatenated capitals.

So I really don't see how you could avoid a double loop.

Only if part.count > 0 is unnecessary.


Alternatively, you can scan the strings yourself to avoid generating the intermediate array fullname.

You can probably also obtain the result by regex processing, but this would be a sledgehammer to crack nuts. Don't.


A possible implementation of the inner loop. (Sorry if this is invalid, I don't know Swift).

var space: Bool = true
for c in name {
  if space && c != ' ' {
    initials+= c
  } 
  space= c == ' '
}

Notice that the (optional) extra condition c != ' ' protects against double spaces.

over 4 years ago · Santiago Trujillo Relatório

0

You can use map to remove some of your own looping

let contacts = ["John Doe", "Mary Blog"]
let result = contacts.map { $0.components(separatedBy: .whitespacesAndNewlines).map { String($0.prefix(1)) }.joined(separator: "") }
print(result)

I don't known if this is any quicker, maybe it gives Apple a chance to fro some optimisation that is hard to do

over 4 years ago · Santiago Trujillo Relatório

0

I did not compare the efficiency of this with the above answers but if you are dealing with names, using PersonNameComponentsFormatter will help you a lot. Check the documentation here.

In your case following code gives the required output.

let formatter = PersonNameComponentsFormatter()

let contacts = ["John Doe", "Mary Blog"]
var initalsArray: [String] = []

for contact in contacts {
    if let name = formatter.personNameComponents(from: contact) {
        formatter.style = .abbreviated
        initalsArray.append(formatter.string(from: name))
    }
}

output -> ["JD", "MB"]

over 4 years ago · Santiago Trujillo Relatório

0

Actually this answer though similar may be a bit quicker

let contacts = ["John Doe", "Mary Blog"]
let result = contacts.map { $0.components(separatedBy: .whitespacesAndNewlines).reduce("") { $0 + String($1.prefix(1)) } }
print(result)

Instated of constructing inner arrays and then joining them I just added it to the string of previous first letters

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda