Compare two arrays and find the index of uncommon elements in SWIFT 3

Bella

Arrays are of type String. Since It is time consuming to add "", I have written it like Int. Sorry.

I have two arrays say var array1 = [[1,2,3,4,5,6,7,8,9]] and

var array2 = [[1,2,3,4],
              [2,3,4,5],
              [2,4,5,6],
              [1,2,3,4,5,6,7,8,9],
              [1,2,3,4,5,6,7,8],
              [2,3,4,5,6,7,8]]

I have to compare each array element of array2 with array1 and insert '-' where the elements do not match. Like this,

var array2 = [[1,2,3,4,-,-,-,-,-],
              [-,2,3,4,5,-,-,-,-],
              [-,2,-,4,5,6,-,-,-],
              [1,2,3,4,5,6,7,8,9],
              [1,2,3,4,5,6,7,8,-],
              [-,2,3,4,5,6,7,8,-]]

I tried to iterate over each array in array2 and compare it with array1, compare the indices and insert '-' to at index position i, but I am getting unexpected results.

UPDATE

 for item in array2{
var elementsArray = item
for i  in stride(from: 0, to: elementsArray.count, by: 1) {
    if elementsArray[i] != array1[i]
    {
        elementsArray.insert("-", at: i)
    }
    print("elemnt array.....", elementsArray, "\n\n")
}
}

I had thought of comparing each array of array2 with array1 by count, find the index of uncommon element and then insert '-' at that index position. Is this approach right? Please help me with this.

Martin R

You want a new array where each row of array2 is replaced by a variant of array1, with elements not originally present in the row replaced by "-":

let array1 = [1,2,3,4,5,6,7,8,9]
let array2 = [[1,2,3,4],
              [2,3,4,5],
              [2,4,5,6],
              [1,2,3,4,5,6,7,8,9],
              [1,2,3,4,5,6,7,8],
              [2,3,4,5,6,7,8]]

let filled = array2.map { row in 
    array1.map {
        row.contains($0) ? String($0) : "-"
    }
}

for row in filled { print(row) }

Output:

["1", "2", "3", "4", "-", "-", "-", "-", "-"]
["-", "2", "3", "4", "5", "-", "-", "-", "-"]
["-", "2", "-", "4", "5", "6", "-", "-", "-"]
["1", "2", "3", "4", "5", "6", "7", "8", "9"]
["1", "2", "3", "4", "5", "6", "7", "8", "-"]
["-", "2", "3", "4", "5", "6", "7", "8", "-"]

For large arrays this can be improved by creating a Set(row) for a faster containment check, or by utilizing that the elements are in increasing order.

Your approach does not work correctly because elementsArray is modified while iterating over it.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Compare arrays, find same elements and return index

How to find uncommon elements from three arrays?

Compare 3 arrays and find common elements between 2 and 3 arrays

Compare two arrays of different structures and find elements in PHP

How to compare two arrays and find the indices where the elements differ?

Compare elements of one array to other two arrays, swift

Python: Compare Elements of two arrays

Compare elements of two arrays of Object

How to compare elements of two arrays

Compare elements of two arrays in javascript

Find different index in arrays compare

Compare Two Different Structured Arrays in Swift3

Compare two arrays and find the differences

Compare the content of two arrays in swift

Find uncommon elements using hashing

How to compare two arrays along and print the common datas along with checkbox checked and uncommon datas with checkbox not checked?

compare two arrays in as3

Is there a way to compare two arrays and return a new array of the common elements which have the same index?

Scala: Compare elements at same position in two arrays

Compare two arrays and print number of matching elements

compare multiple arrays for same elements in swift

How to compare two arrays and then return the index of the difference?

How to compare two arrays and remove matching elements from one array in Swift iOS

Compare two list and get the index of same elements

Compare two object arrays and find the indices

How to compare two arrays of objects to find the differences

How to compare two arrays of protocols for equality in Swift?

Compare Two Arrays and Remove Duplicates Swift

Compare each element in two arrays Swift