-3
text = ["lorem", "ipsum", "dolor", "sit", "amet"]

after random:

wrongText = ["lorem", "ipsum", "amet", "sit", "dolor"]

How do i know which word was misplaced ? In example above i need only "amet" and "dolor"

Jay
  • 83
  • 1
  • 1
  • 8
  • 1
    Before posting do some research it may help you https://stackoverflow.com/questions/36714522/how-do-i-check-in-swift-if-two-arrays-contain-the-same-elements-regardless-of-th – Dharma Jun 21 '17 at 13:35

3 Answers3

4

Try something like:

let changed = zip(text,wrongText).filter { $0 != $1 }.map { $0.0 }

This will pair the elements of both arrays and output the non-matching pairs. The map will extract the first word of each non-matching pair, since you'll only need that.

Matusalem Marques
  • 2,399
  • 2
  • 18
  • 28
0
var changed = [String]()    
for i in 0..<text.count {
      if text[i] != wrongText[i]{
         changed.append(wrongText[i])
      }            
}
colmlg
  • 193
  • 6
0

A shorter version that takes advantage of flatMap as nil filter:

let changed = zip(text, wrongText).flatMap { $0 != $1 ? $0 : nil }
Code Different
  • 90,614
  • 16
  • 144
  • 163