【问题标题】:In scala, how can I get the count of elements that never shown in both arrays?在 scala 中,如何获取两个数组中从未显示的元素的数量?
【发布时间】:2019-08-24 20:46:40
【问题描述】:

例如,我有一个数组 Array[Int] = Array(1, 1, 2, 2, 3) 数组 b Array[Int] = Array(2, 3, 4, 5) 我想计算仅在 a 或 b 中显示的元素数量。在这种情况下,它是 (1, 1, 4, 5),所以计数是 4。

我尝试了 diff、union、intersect,但我找不到它们的组合来获得我想要的结果。

【问题讨论】:

  • 差异集唯一值的长度

标签: arrays scala dataframe diff intersect


【解决方案1】:

我认为您可以尝试类似的方法,但这不是好方法,但这仍然可以解决问题。

a.filterNot(b contains).size + b.filterNot(a contains).size

【讨论】:

    【解决方案2】:

    与其他答案相同,但线性时间:

     a.iterator.filterNot(b.toSet).size + b.iterator.filterNot(a.toSet).size
    

    .iterator 避免创建中间集合)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-24
      • 2017-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多