0
votes

I have an array 'board_matrix' with some values in it. And I also have another array 'win' with some indices values. Now I want to make 'board_matrix' go all zero except those indices in 'win' array.

say,

board_matrix = [1,0,2,2,1,0,1,0,1]
win = [0,4,8]

then output should be 
new_array = [1,0,0,0,1,0,0,0,1]
2
If you have win indices, why not to create a new array to alter the values based on comparing the values on specified indices... - Mohit Kumar
I too want to create a new array , but I can't compare the value of indices - Madhur
Can you change win to a Set<Int>? Then you can iterate over the array and use win.contains(index) - Paulw11

2 Answers

0
votes

You can iterate over the board_matrix array and overwrite the values if the index is not found in the win array. Something like this:

for (i=0; i<board_matrix.size; i++) {
    if (!win.contains(i)) board_matrix[i] = 0;
}
0
votes
board_matrix.enumerated().map { [winIndices = Set(win)] in
  winIndices.contains($0.offset) ? $0.element : 0
}