1

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.

13
  • Is array1 supposed to be a one dimensional array or an array of arrays? In the latter case should the i-th subarray of array1 be compared to the i-th subarray of array2? Commented Aug 17, 2017 at 13:11
  • About each array in array2, can it's length be greater than the length of array1? Commented Aug 17, 2017 at 13:20
  • @DávidPásztor array1 is one dimensional only. Commented Aug 17, 2017 at 13:22
  • Are the numbers from the second array always present in the first array? Are the numbers always increasing? Commented Aug 17, 2017 at 13:23
  • @Adeel no It cannot be greater. Max length of array present in array2 is equal to array1 Commented Aug 17, 2017 at 13:24

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Yippie...!! It worked... Thanks a alot. :) Based on array1 and array2 result there is one more array array3 with different data. The problem is I have to set the values of array3 like array2 by matching it each element of the array2. Please help.
@Bella: I have tried to answer your question. If you have an additional problem, please try to solve it yourself first, with the tools provided in this answer. If necessary, ask a new question.
Sorry. What if there is no array1 And I have to solve problem only for array2. I am trying by best to solve it, but not getting proper way
I think to solve that without array1 you need to add an step to find which array is bigger and take as reference in this case your bigger array in array2 will be your array1 @Bella

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.