Swift Set isDisjoint()

isDisjoint() 方法如果两个集合是互斥集,则返回 true。如果不是,则返回 false。

示例

var A: Set = [1, 2, 3, 4, 5]
var B: Set = [12,13,14]

// check if A is disjoint with B or not print(A.isDisjoint(with: B))
// Output: true

isDisjoint() 语法

集合 isDisjoint() 方法的语法是

set.isDisjoint(otherSet)

其中,setSet 类的一个对象。


isDisjoint() 参数

isDisjoint() 方法接受一个参数

  • otherSet - 元素的集合。

isDisjoint() 返回值

  • 如果 setotherSet 互斥,则 isDisjoint() 方法返回 true。如果不是,则返回 false

示例:Swift Set isDisjoint()

var A: Set = [1, 2, 3, 4]
var B: Set = [5, 6, 7]
var C: Set = [4, 5, 6]

// check if A and B are disjoint or not print("Are A and B disjoint?", A.isDisjoint(with: B))
// check if A and C are disjoint or not print("Are A and C disjoint?", A.isDisjoint(with: C))

输出

Are A and B disjoint? true
Are A and C disjoint? false

在这里,我们使用 isDisjoint() 方法来检查两个集合是否互斥。

由于

  • AB 拥有唯一的元素,该方法返回 true
  • AC 都拥有元素 4,该方法返回 false
你觉得这篇文章有帮助吗?

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战