Jaccard Similarity Explained with Examples

Jaccard similarity is the share of elements two sets have in common. We work through numbers, sentences and binary data, and see where the measure falls short.
Statistics
Programming
Explainer
Author

Abdullah Al Mahmud

Published

December 1, 2026

Venn diagram of the 2-letter shingles of sunday and monday. Only sunday has su and un, only monday has mo and on, and both have nd, da and ay. The Jaccard similarity is 3 over 7, about 0.43.

The 2-letter shingles of “sunday” and “monday”: three of the seven are shared.

Jaccard similarity (also called the Jaccard index or Jaccard coefficient) measures how similar two sets are. It is the size of their intersection divided by the size of their union:

\[ J(A, B) = \frac{|A \cap B|}{|A \cup B|} \]

Since \(|A \cup B| = |A| + |B| - |A \cap B|\), we only need the sizes of the two sets and of their overlap.

Reading the value

  • \(J\) always lies between 0 and 1.
  • \(J = 1\): the sets are identical.
  • \(J = 0\): the sets share no elements.
  • If both sets are empty, the ratio is \(0/0\), so we have to define it by convention.

The matching measure of dissimilarity is the Jaccard distance:

\[ d_J(A, B) = 1 - J(A, B) \]

Example 1: two words

A 2-shingle is a pair of neighbouring letters. We break two words, “sunday” and “monday”, into their sets of 2-shingles, as in the figure above:

\[ \begin{aligned} A &= \{\text{su, un, nd, da, ay}\} \\ B &= \{\text{mo, on, nd, da, ay}\} \end{aligned} \]

They share nd, da and ay. Each set has five shingles, so the union holds \(5 + 5 - 3 = 7\):

\[ J(A, B) = \frac{3}{7} \approx 0.429 \]

Example 2: sets of numbers

Let \(A = \{1, 2, 3, 4\}\) and \(B = \{3, 4, 5, 6\}\). Then:

\[ \begin{aligned} A \cap B &= \{3, 4\} \\ A \cup B &= \{1, 2, 3, 4, 5, 6\} \\ J(A, B) &= \frac{2}{6} \approx 0.333 \end{aligned} \]

The Jaccard distance is therefore about \(0.667\).

Example 3: two sentences

To compare texts, we can treat each one as a set of unique words. Word order and repeated words are then ignored.

  • Sentence 1: “Data mining finds hidden patterns”
  • Sentence 2: “Machine learning finds useful patterns”

\[ \begin{aligned} A &= \{\text{data, mining, finds,} \\ &\qquad \text{hidden, patterns}\} \\ B &= \{\text{machine, learning, finds,} \\ &\qquad \text{useful, patterns}\} \end{aligned} \]

The sentences share two words, finds and patterns. Each has five words, so the union holds \(5 + 5 - 2 = 8\) words:

\[ J(A, B) = \frac{2}{8} = 0.25 \]

Example 4: binary data

Data is often coded as 1 (present) and 0 (absent). Suppose we record which of seven movies two users have watched:

Movie 1 2 3 4 5 6 7
User A 1 1 0 1 0 1 0
User B 1 0 0 1 1 1 0

We count the positions of each kind:

  • \(M_{11} = 3\): both watched (movies 1, 4 and 6)
  • \(M_{10} = 1\): only A watched (movie 2)
  • \(M_{01} = 1\): only B watched (movie 5)

\[ \begin{aligned} J &= \frac{M_{11}}{M_{11} + M_{10} + M_{01}} \\[4pt] &= \frac{3}{3 + 1 + 1} = 0.60 \end{aligned} \]

Note

Movies 3 and 7, which neither user watched, are left out. Otherwise shared absences would inflate the similarity, especially when most entries are 0.

The same in R and Python

We write one function to cut a word into 2-shingles and another to compare the two sets, then repeat Example 1:

shingles <- function(word) {
  n <- nchar(word)
  unique(substring(word, 1:(n - 1), 2:n))
}

jaccard <- function(a, b) {
  length(intersect(a, b)) / length(union(a, b))
}

A <- shingles("sunday")
B <- shingles("monday")

intersect(A, B)
jaccard(A, B)
[1] "nd" "da" "ay"
[1] 0.4285714
def shingles(word):
    return {word[i:i + 2] for i in range(len(word) - 1)}

def jaccard(a, b):
    return len(a & b) / len(a | b)

A = shingles("sunday")
B = shingles("monday")

print(sorted(A & B))
print(jaccard(A, B))
['ay', 'da', 'nd']
0.42857142857142855

Both give \(3/7\). The Python version sorts the shared shingles before printing, because a set has no fixed order.

Where it is used

  • Near-duplicate and plagiarism detection: we break documents into short pieces and estimate \(J\) quickly with MinHashing, then use Locality-Sensitive Hashing to avoid comparing every pair.
  • Recommendation systems: comparing the sets of items that users liked or bought.
  • Computer vision: Intersection over Union (IoU), used to score object detection, is Jaccard similarity applied to image regions.
  • Ecology: comparing the species found at two sites.

Key limitation

A set only records whether an element is present. Jaccard similarity therefore treats rare and common elements alike, and ignores how often each one occurs. When counts or weights matter, cosine similarity is often a better choice.

References

R-bloggers

R posts from Stat Mania are also shared on R-bloggers, a hub of R blogs.