Administrator
发布于 2022-04-06 / 566 阅读 / 0 评论 / 0 点赞

取切片差集


func _arrayDiff(array1 []int, othersParams []int) ([]int, error) {
   if len(array1) == 0 {
      return []int{}, nil
   }
   if len(array1) > 0 && len(othersParams) == 0 {
      return array1, nil
   }
   var tmp = make(map[string]int, len(array1))
   for i, v := range array1 {

      // 唯一key
      key := fmt.Sprintf("%d-%d",i,v)
      tmp[key] = v
   }

   for _, arg := range othersParams {
      for i,v := range tmp{
         if v != -1 && v == arg{
            tmp[i] = -1
            break
         }
      }
   }

   var res = make([]int, 0, len(tmp))
   for _, v := range tmp {
      if v != -1 {
         res = append(res, v)
      }
   }
   return res, nil
}