iOS swift remove elements of an array from another array

前端 未结 6 828
有刺的猬
有刺的猬 2020-12-13 12:24

I have two arrays

var array1 = new Array [\"a\", \"b\", \"c\", \"d\", \"e\"]
var array2 = new Array [\"a\", \"c\", \"d\"]

I want to remove

相关标签:
6条回答
  • 2020-12-13 12:25

    out of scope but it would help me if it had been here. removing subArray from array in OBJECTIVE-C

    NSPredicate* predicate = [NSPredicate predicateWithFormat:@"not (self IN %@)", subArrayToBeDeleted];
    NSArray* finalArray = [initialArray filteredArrayUsingPredicate:predicate];
    

    hope it will ever help someone :)

    0 讨论(0)
  • 2020-12-13 12:25

    You can create sets and then use the subtract method

    let setA = Set(arr1)
    let setB = Set(arr2)
    setA.subtract(setB)
    
    0 讨论(0)
  • 2020-12-13 12:29

    The easiest way is to convert both arrays to sets, subtract the second from the first, convert to the result to an array and assign it back to array1:

    array1 = Array(Set(array1).subtracting(array2))
    

    Note that your code is not valid Swift - you can use type inference to declare and initialize both arrays as follows:

    var array1 = ["a", "b", "c", "d", "e"]
    var array2 = ["a", "c", "d"]
    
    0 讨论(0)
  • 2020-12-13 12:33

    @Antonio's solution is more performant, but this preserves ordering, if that's important:

    var array1 = ["a", "b", "c", "d", "e"]
    let array2 = ["a", "c", "d"]
    array1 = array1.filter { !array2.contains($0) }
    
    0 讨论(0)
  • 2020-12-13 12:38

    Here is an extension with @jrc answer:

    extension Array where Element: Equatable {
        func subtracting(_ array: Array<Element>) -> Array<Element> {
            self.filter { !array.contains($0) }
        }
    }
    
    0 讨论(0)
  • 2020-12-13 12:50

    Remove elements using indexes array:

    1. Array of Strings and indexes

      let animals = ["cats", "dogs", "chimps", "moose", "squarrel", "cow"]
      let indexAnimals = [0, 3, 4]
      let arrayRemainingAnimals = animals
          .enumerated()
          .filter { !indexAnimals.contains($0.offset) }
          .map { $0.element }
      
      print(arrayRemainingAnimals)
      
      //result - ["dogs", "chimps", "cow"]
      
    2. Array of Integers and indexes

      var numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
      let indexesToRemove = [3, 5, 8, 12]
      
      numbers = numbers
          .enumerated()
          .filter { !indexesToRemove.contains($0.offset) }
          .map { $0.element }
      
      print(numbers)
      
      //result - [0, 1, 2, 4, 6, 7, 9, 10, 11]
      



    Remove elements using element value of another array

    1. Arrays of integers

      let arrayResult = numbers.filter { element in
          return !indexesToRemove.contains(element)
      }
      print(arrayResult)
      
      //result - [0, 1, 2, 4, 6, 7, 9, 10, 11]
      
    2. Arrays of strings

      let arrayLetters = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
      let arrayRemoveLetters = ["a", "e", "g", "h"]
      let arrayRemainingLetters = arrayLetters.filter {
          !arrayRemoveLetters.contains($0)
      }
      
      print(arrayRemainingLetters)
      
      //result - ["b", "c", "d", "f", "i"]
      
    0 讨论(0)
提交回复
热议问题