Jaccard Similarity

EasyMachine LearningMathArray

Description

Given two arrays treated as sets, return their Jaccard similarity: the size of the intersection divided by the size of the union. If both sets are empty, return 0. Round the result to 4 decimal places.

Examples

Input:[1,2,3], [2,3,4]
Output:0.5
Explanation:

The two sets share 2 common values across 4 distinct values in their union, and dividing those two counts gives the Jaccard similarity.

Input:[1,2,3], [1,2,3]
Output:1
Explanation:

The two sets share 3 common values across 3 distinct values in their union, and dividing those two counts gives the Jaccard similarity.

Input:[1,2], [3,4]
Output:0
Explanation:

The two sets share 0 common values across 4 distinct values in their union, and dividing those two counts gives the Jaccard similarity.

Constraints

  • 0 ≤ array length ≤ 10³

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.