title: Fastest way to find and replace multiple items in an array
body: <p>I have two arrays with about 1000 prices and sizes in order from smallest to largest called <code>tempArrayBuy</code> and <code>tempArraySell</code>. </p>
  
  <pre><code>let tempArrayBuy = [Orders]()
  let tempArraySell = [Orders]()
  
  struct Orders:Codable {
      let price : Double
      let size : Double
  
      init(from decoder: Decoder) throws {
          var container = try decoder.unkeyedContainer()
          price = Double(try container.decode(String.self)) ?? 0.0
          size = Double(try container.decode(String.self)) ?? 0.0
      }
  
      init(_ orderEntryChange: OrderChangesFeed) {
          price = orderEntryChange.price ?? 0.0
          size = orderEntryChange.size ?? 0.0
      }
  
  }
  </code></pre>
  
  <p>I am getting changes that need to be made to the two arrays above through a websocket feed. I need to find the correct price in the <code>tempArrayBuy</code> and the replace that quantity with the quantity in the <code>OrderBookChangesFeed</code> item. </p>
  
  <p>Here is the format of the data from the websocket. I have an array of changes. </p>
  
  <pre><code>let changesNeeded = [OrderChangesFeed]()
  </code></pre>
  
  <p>This is the structure of the data. </p>
  
  <pre><code>struct OrderChangesFeed : Codable {
      let side:String        
      let price : Double
      let size : Double
  
      init(from decoder: Decoder) throws {
          var container = try decoder.unkeyedContainer()
          side = try container.decode(String.self)
          price = Double(try container.decode(String.self)) ?? 0.0
          size = Double(try container.decode(String.self)) ?? 0.0
      }
  }
  </code></pre>
  
  <p>If the side is <code>buy</code> then it needs to update the <code>buyTempArray</code> and if it is <code>sell</code> it needs to update the <code>sellTempArray</code>. If the price from the change websocket (<code>OrderBookChangesFeed</code>) does not exist in the array, it needs to be added with the quantity/size given and if the quantity/size associated to a specific price in the changes websocket is 0 then it needs to be removed from the tempArray. </p>
  
  <p>Here is what I am doing now. But I feel like there is a faster way to do this. I am getting about 10-20+ changes in a second so speed is the most important thing here. </p>
  
  <pre><code>for item in changesNeeded {
      if item.side == "buy"{
          if let tempInex = self.tempArrayBuy.index(where: {$0.price==item.price}) {
              if item.size == 0 {
                  self.tempArrayBuy.remove(at: tempInex)
              }else {
                  self.tempArrayBuy[tempInex] = Orders(item)
              }             
          }else {
              //Insert at the right spot in array. The array is sorted numerically by price. I am not sure what a fast way to do this would be.
          }
      }else {
          //Do the same thing for sell side
      }
  }
  </code></pre>
  
  <p>I also am not sure how to insert it in the right spot numerically. Any help on this would be amazing!</p>