{
  "id": "set-difference",
  "title": "Set Difference",
  "lang": "en",
  "source_lang": "en",
  "summary": "Set difference is the operation that forms the set of elements belonging to one set but not to another.",
  "category": "mathematics",
  "subcategory": "discrete-logic",
  "type": "concept",
  "aliases": [
    "Relative complement",
    "Difference of sets"
  ],
  "generated_at": "2026-11-05",
  "model": "pro/gpt-6.1-sol",
  "url": "https://aiwiki.page/en/set-difference/",
  "translations": {
    "zh-Hans": {
      "url": "https://aiwiki.page/zh/set-difference/",
      "json": "https://aiwiki.page/zh/set-difference.json"
    }
  },
  "sections": [
    {
      "heading": "",
      "text": "Set difference is an operation in set theory that selects the elements of one set that do not belong to another. For sets \\(A\\) and \\(B\\), the difference \\(A\\setminus B\\), read “\\(A\\) minus \\(B\\),” contains precisely those elements that are in \\(A\\) and not in \\(B\\). It is also called the relative complement of \\(B\\) in \\(A\\). The order matters: in general, \\(A\\setminus B\\) and \\(B\\setminus A\\) are different sets."
    },
    {
      "heading": "Definition and notation",
      "text": "The defining membership condition is\n\n\\[A\\setminus B=\\{x\\in A:x\\notin B\\}.\\]\n\nEquivalently,\n\n\\[x\\in A\\setminus B \\quad\\Longleftrightarrow\\quad x\\in A\\ \\text{and}\\ x\\notin B.\\]\n\nThe notation \\(A-B\\) is also common, although \\(\\setminus\\) distinguishes set difference from numerical subtraction and other operations.\n\nThere is no requirement that \\(B\\) be a subset of \\(A\\). Elements of \\(B\\) that are outside \\(A\\) have no effect on the result. In particular,\n\n\\[A\\setminus B=A\\setminus(A\\cap B),\\]\n\nwhere \\(\\cap\\) denotes intersection.\n\nFor example, if\n\n\\[A=\\{1,2,3,4\\},\\qquad B=\\{3,4,5\\},\\]\n\nthen\n\n\\[A\\setminus B=\\{1,2\\},\\qquad B\\setminus A=\\{5\\}.\\]\n\nIn a Venn diagram, \\(A\\setminus B\\) is represented by the portion of the region for \\(A\\) lying outside the region for \\(B\\)."
    },
    {
      "heading": "Relationship to complements and other operations",
      "text": "If a fixed ambient set \\(U\\) contains both \\(A\\) and \\(B\\), the complement of \\(B\\) in \\(U\\) is \\(B^c=U\\setminus B\\). Set difference can then be written as\n\n\\[A\\setminus B=A\\cap B^c.\\]\n\nUnlike the notation \\(B^c\\), which requires an understood ambient set, \\(A\\setminus B\\) specifies the set within which exclusion takes place. The word “complement” without qualification often means complement in a fixed ambient set; “relative complement” makes that dependence explicit.\n\nSet difference also determines the symmetric difference:\n\n\\[A\\mathbin{\\triangle}B =(A\\setminus B)\\cup(B\\setminus A),\\]\n\nwhere \\(\\cup\\) denotes union. Symmetric difference contains elements belonging to exactly one of the two sets. Unlike ordinary set difference, it is commutative."
    },
    {
      "heading": "Basic properties",
      "text": "Inclusion and empty sets\n\nFor all sets \\(A\\) and \\(B\\),\n\n\\[A\\setminus B\\subseteq A, \\qquad (A\\setminus B)\\cap B=\\varnothing.\\]\n\nHere \\(\\varnothing\\) is the empty set. The basic identities are\n\n\\[A\\setminus\\varnothing=A,\\qquad \\varnothing\\setminus A=\\varnothing,\\qquad A\\setminus A=\\varnothing.\\]\n\nSet difference characterizes inclusion and disjointness:\n\n\\[A\\setminus B=\\varnothing \\quad\\Longleftrightarrow\\quad A\\subseteq B,\\]\n\nand\n\n\\[A\\setminus B=A \\quad\\Longleftrightarrow\\quad A\\cap B=\\varnothing.\\]\n\nThe sets \\(A\\setminus B\\) and \\(A\\cap B\\) give a disjoint decomposition of \\(A\\):\n\n\\[A=(A\\setminus B)\\cup(A\\cap B).\\]\n\nThus every element of \\(A\\) belongs to exactly one of these two parts.\n\nDependence on each argument\n\nSet difference preserves inclusion in its first argument:\n\n\\[A\\subseteq C \\quad\\Longrightarrow\\quad A\\setminus B\\subseteq C\\setminus B.\\]\n\nIt reverses inclusion in its second argument:\n\n\\[B\\subseteq C \\quad\\Longrightarrow\\quad A\\setminus C\\subseteq A\\setminus B.\\]\n\nEnlarging the set from which elements are taken can enlarge the result; enlarging the set being excluded can shrink it.\n\nFailure of commutativity and associativity\n\nSet difference is generally neither commutative nor associative:\n\n\\[A\\setminus B\\ne B\\setminus A\\]\n\nin general, and parentheses cannot ordinarily be omitted from repeated differences.\n\nFor example, let \\(A=B=C=\\{1\\}\\). Then\n\n\\[(A\\setminus B)\\setminus C=\\varnothing, \\qquad A\\setminus(B\\setminus C)=\\{1\\}.\\]\n\nThe two parenthesizations satisfy different identities:\n\n\\[(A\\setminus B)\\setminus C=A\\setminus(B\\cup C),\\]\n\nwhereas\n\n\\[A\\setminus(B\\setminus C) =(A\\setminus B)\\cup(A\\cap C).\\]"
    },
    {
      "heading": "Identities involving unions and intersections",
      "text": "Set difference obeys the following identities:\n\n\\[(A\\cup B)\\setminus C =(A\\setminus C)\\cup(B\\setminus C),\\]\n\n\\[(A\\cap B)\\setminus C =(A\\setminus C)\\cap(B\\setminus C),\\]\n\n\\[A\\setminus(B\\cup C) =(A\\setminus B)\\cap(A\\setminus C),\\]\n\n\\[A\\setminus(B\\cap C) =(A\\setminus B)\\cup(A\\setminus C).\\]\n\nThe last two are relative forms of De Morgan’s laws. They express the distinction between excluding everything in either of two sets and excluding only what lies in both.\n\nFor a nonempty indexed family \\((B_i)_{i\\in I}\\), they generalize to\n\n\\[A\\setminus\\bigcup_{i\\in I}B_i =\\bigcap_{i\\in I}(A\\setminus B_i),\\]\n\nand\n\n\\[A\\setminus\\bigcap_{i\\in I}B_i =\\bigcup_{i\\in I}(A\\setminus B_i).\\]"
    },
    {
      "heading": "Cardinality and measure",
      "text": "For finite sets, the cardinality of a difference is\n\n\\[|A\\setminus B|=|A|-|A\\cap B|.\\]\n\nIf \\(B\\subseteq A\\), this simplifies to\n\n\\[|A\\setminus B|=|A|-|B|.\\]\n\nWithout the subset condition, subtracting \\(|B|\\) would incorrectly count elements of \\(B\\) that were never in \\(A\\).\n\nFor infinite sets, cardinalities do not admit a corresponding general subtraction operation. Two sets may have the same infinite cardinality while their difference has different possible sizes. For example, if \\(A\\) is the set of natural numbers, taking \\(B=A\\), taking \\(B\\) to be \\(A\\) with one element removed, or taking \\(B\\) to be the even natural numbers makes \\(A\\setminus B\\) empty, a singleton, or infinite, respectively.\n\nIn measure theory, measurable sets satisfy\n\n\\[\\mu(A)=\\mu(A\\setminus B)+\\mu(A\\cap B).\\]\n\nIf \\(\\mu(A)<\\infty\\), this gives\n\n\\[\\mu(A\\setminus B)=\\mu(A)-\\mu(A\\cap B).\\]\n\nThe finiteness condition prevents an undefined subtraction of the form \\(\\infty-\\infty\\).\n\nFor events in probability theory, the corresponding formula is always valid:\n\n\\[P(A\\setminus B)=P(A)-P(A\\cap B).\\]\n\nThe event \\(A\\setminus B\\) means that \\(A\\) occurs and \\(B\\) does not."
    },
    {
      "heading": "Logical and computational interpretations",
      "text": "Using an indicator function, set difference has the pointwise expression\n\n\\[\\mathbf{1}_{A\\setminus B}(x) =\\mathbf{1}_A(x)\\bigl(1-\\mathbf{1}_B(x)\\bigr).\\]\n\nThis connects set operations with Boolean algebra: membership in the result is the logical conjunction of membership in \\(A\\) and nonmembership in \\(B\\).\n\nIn computing, an algorithm for set difference retains each element of the first collection only if it is absent from the second. Implementations depend on the representation: hash-based sets use membership tests, sorted representations can be compared by a merge-like scan, and bit-vector representations use bitwise AND with the complement of the second vector.\n\nMathematical sets do not record repeated occurrences. Consequently, set difference must be distinguished from operations on lists or multisets, where order or multiplicity may matter. Database systems similarly distinguish ordinary set-style difference from variants that account for duplicate rows."
    }
  ],
  "links": [
    "set-theory",
    "set-mathematics",
    "subset",
    "set-intersection",
    "set-complement",
    "symmetric-difference",
    "set-union",
    "empty-set",
    "de-morgans-laws",
    "cardinality",
    "measure-theory",
    "event-probability",
    "probability",
    "indicator-function",
    "boolean-algebra",
    "algorithm"
  ],
  "sources": [],
  "license": "https://creativecommons.org/licenses/by/4.0/"
}