Swift 3 comparing array indexes


If i have two arrays & i want to compare their indexes, for ex:

let var a1 = ["1", "2", "3"] let var a2 = ["3", "2", "3"]

And i wanted to print something to say which index wasn't the same, such as:

if a1[0] != a2[0] && a1[1] == a2[1] && a1[2] == a2[2]{ print("Index 0 is not the same.")

Would i have to write 7 more of those statements to show all 8 possibilities of all correct/all wrong/index 1&1 wrong, etc?

Thank you!


You can get all indexes like this:

let diffIndex = zip(a1, a2).enumerated().filter {$1.0 != $1.1}.map {$0.offset}


<ul><li>zip produces a sequence of pairs</li> <li>enumerated() adds an index to the sequence</li> <li>filter keeps only pairs with different values</li> <li>map harvests the index, and builds the sequence of results.</li> </ul>

Running this on

let a1 = ["1", "2", "3", "4"] let a2 = ["3", "2", "3", "5"]

This produces a sequence [0, 3]


Use a for loop:

for i in 0..<a1.length { if a1[i] != a2[i] { print("Index \(i) is not the same") } }

Generally if you find yourself repeating the same code but with different numbers, you can substitute it with a for loop.


Try this

let a1 = ["1", "2", "3"] let a2 = ["3", "2", "3"] let result = zip(a1, a2).map({ $0 == $1 }).reduce(true, {$0 && $1})


Surely you could do something like this:

let a1 = ["1", "2", "3"] let a2 = ["3", "2", "3"] var compareResult : [String] = [String]() if a1.count == a2.count { // Need to check they have same length for count in 0..<a1.count { let result : String = a1[count] == a2[count] ? "MATCH" : "MISMATCH" compareResult.append(result) } print(compareResult) // Do something more interesting with compare result... }


  • How to use an .net assembly installed in GAC in my project
  • Using Python to group csv data
  • Why shouldn't an object be cloneable? [closed]
  • Python: matching values from one list to the sequence of values in another list
  • PyTorch: Relation between Dynamic Computational Graphs - Padding - DataLoader
  • NHibernate: Persist an object with children
  • “Drawing” an arc in discrete x-y steps
  • Select values within/outside of a set of intervals (ranges) R
  • Root element minOccurs or maxOccurs
  • How to get the max timestamp and groupby another field in LINQ to Entities
  • Shuffling a 2D array without using Collections
  • Get last insert id of Postgresql
  • Is it possible to generate a unique numeric value for each row in an iSeries table without looping?
  • C++ - Is destructor called when a vector holds objects?
  • CoreData basics – to-many relationship array data
  • Eloquent Relationship on Partial Match
  • XMLBeans Duplicate Class and Class Not Found Errors
  • Accessing Rows In A LINQ Result Without A Foreach Loop?
  • Web.config system.webserver errors
  • JBoss External Properties Files in Classpath
  • How to add git credentials to the build so it would be able to be used within a shell code?
  • Mysterious problem with floating point in LISP - time axis generation
  • Bad request using file_get_contents for PUT request in PHP
  • Meteor: Do Something On Email Verification Confirmation
  • Spring security and special characters
  • SSO with signing and signature validation doesn't work
  • Deserializing XML into class C#
  • Where to put my custom functions in Wordpress?
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • 0x202A in filename: Why?
  • RestKit - RKRequestDelegate does not exist
  • retrieve vertices with no linked edge in arangodb
  • json Serialization in asp
  • WPF Applying a trigger on binding failure
  • Why joiner is not used after Sequence generator or Update statergy
  • File not found error Google Drive API
  • Linking SubReports Without LinkChild/LinkMaster
  • Programmatically clearing map cache
  • costura.fody for a dll that references another dll
  • Python/Django TangoWithDjango Models and Databases