1

Func to see if two arrays have some same values:

func hasAllSame(largeCombinedArry:[Int], wantToKeep: [Int])->Bool{
   var howManySame = [Int]()
   for intElement in largeCombinedArry{
      if wantToKeep.contains(intElement){
         howManySame.append(intElement)
      }
   }
   howManySame.sort()
   if wantToKeep == howManySame{
      print("These are the same!")
      return true
   }
   else{
      print("These are NOT same!")
      return false
   }
}

Array of tuples declared like this:

var TuplesArry:[(score: Double, value: [Int])] = []

Array filled thusly:

for (arry1, score) in zip(arryOfArrays, AllScores) {
   let calculatedDiff = otherValue - score
   TuplesArry.append((score: calculatedDiff, value: arry1))
}

var arrayForComparison = [8,9,7,6]
arrayForComparison.sort()

Error occurs here after several iteration at function call hasAllSame()

for i in 0..<TuplesArry.count{
   if hasAllSame(largeCombinedArry: TuplesArry[i].value, wantToKeep:                                                                  
   arrayForComparison){
      //do nothing           
   }
   else{
      /*
       want to remove tuple that does not have all elements that are
       in arrayForComparison
       */
      TuplesArry.remove(at: i)                    
   }
}

This code seems to be working but it seems like tuplesArry.count continues to decrease and iterator i continues to increase until error occurs "fatal index out of range"

My goal is to remove a tuple from the array of tuples if it's value does not meet criteria.

I've also tried something like:

for tuple in TuplesArry{
   if hasAllSame(largeCombinedArry: tuple.value, wantToKeep:   
   arrayForComparison){
            //do nothing
   }
   else{
      //this does NOT work
      let index = TuplesArry.index(of:tuple)
      TuplesArry.remove(at: index)
   }
}
2
  • Please consider this note in the Language Guide: Tuples are useful for temporary groups of related values. They’re not suited to the creation of complex data structures. If your data structure is likely to persist beyond a temporary scope, model it as a class or structure, rather than as a tuple. Commented Nov 8, 2017 at 15:51
  • @vadian I took this into consideration and implemented it as class. Thank you for your feedback Commented Nov 14, 2017 at 10:26

1 Answer 1

1

The immediate issue is that you need to iterate in reverse to avoid the "index out of range" issue.

The much simpler solution is to use filter on your array. Then you entire for loop can be replaced with:

TouplesArry = TouplesArry.filter { hasAllSame(largeCombinedArry: $0.value, wantToKeep: arrayForComparison) }
Sign up to request clarification or add additional context in comments.

Comments

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.