TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

Jaccard Similarity

Recommender Systems
Easy

Jaccard similarity measures the overlap between two collections after duplicates are removed. Form sets A and B from the input lists, then compute

J(A,B)=∣A∩B∣∣A∪B∣J(A, B) = \frac{|A \cap B|}{|A \cup B|}J(A,B)=∣A∪B∣∣A∩B∣​

Here, the numerator counts items shared by both sets, while the denominator counts distinct items present in either set. If both sets are empty, return 0.0. Otherwise return the similarity as a float.

Loading visualization...

Examples

Input: set_a = [1, 2, 3], set_b = [2, 3, 4]

Output: 0.500000

Explanation: The intersection has two items and the union has four, giving 2 / 4 = 0.5.

Input: set_a = [1, 2], set_b = [3, 4]

Output: 0.000000

Hint 1

Convert both lists to sets before measuring overlap.

Hint 2

Handle an empty union before performing the division.

Requirements

  • Treat duplicate input items as a single item.
  • Divide the intersection size by the union size.
  • Return 0.0 when both sets are empty.
  • Return a float from 0.0 through 1.0.

Constraints

  • set_a and set_b are lists of integers.
  • Either input list may be empty.
  • Time limit: 300 ms.
Try Similar Problems
Cosine SimilarityAdjusted Cosine SimilarityEdit DistanceTop K RecommendationsDot Product

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: array

You must run your code first.
PrevNext

Jaccard Similarity

Recommender Systems
Easy

Jaccard similarity measures the overlap between two collections after duplicates are removed. Form sets A and B from the input lists, then compute

J(A,B)=∣A∩B∣∣A∪B∣J(A, B) = \frac{|A \cap B|}{|A \cup B|}J(A,B)=∣A∪B∣∣A∩B∣​

Here, the numerator counts items shared by both sets, while the denominator counts distinct items present in either set. If both sets are empty, return 0.0. Otherwise return the similarity as a float.

Loading visualization...

Examples

Input: set_a = [1, 2, 3], set_b = [2, 3, 4]

Output: 0.500000

Explanation: The intersection has two items and the union has four, giving 2 / 4 = 0.5.

Input: set_a = [1, 2], set_b = [3, 4]

Output: 0.000000

Hint 1

Convert both lists to sets before measuring overlap.

Hint 2

Handle an empty union before performing the division.

Requirements

  • Treat duplicate input items as a single item.
  • Divide the intersection size by the union size.
  • Return 0.0 when both sets are empty.
  • Return a float from 0.0 through 1.0.

Constraints

  • set_a and set_b are lists of integers.
  • Either input list may be empty.
  • Time limit: 300 ms.
Try Similar Problems
Cosine SimilarityAdjusted Cosine SimilarityEdit DistanceTop K RecommendationsDot Product

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: array

You must run your code first.