Find the Jaccard Index and Jaccard Distance between the two given sets

Last Updated : 29 Aug, 2022

Given two sets of integers s1 and s2, the task is to find the Jaccard Index and the Jaccard Distance between the two sets. Examples:

Input: s1 = {1, 2, 3, 4, 5}, s2 = {4, 5, 6, 7, 8, 9, 10} Output: Jaccard index = 0.2 Jaccard distance = 0.8 Input: s1 = {1, 2, 3, 4, 5}, s2 = {4, 5, 6, 7, 8} Output: Jaccard index = 0.25 Jaccard distance = 0.75

Approach: The Jaccard Index and the Jaccard Distance between the two sets can be calculated by using the formula:

Below is the implementation of the above approach:

C++

 // C++ implementation of the approach#include using namespace std; // Function to return the// intersection set of s1 and s2set<int> intersection(set<int> s1, set<int> s2){    set<int> intersect;     // Find the intersection of the two sets    set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(),                     inserter(intersect, intersect.begin()));     return intersect;} // Function to return the Jaccard index of two setsdouble jaccard_index(set<int> s1, set<int> s2){    // Sizes of both the sets    double size_s1 = s1.size();    double size_s2 = s2.size();     // Get the intersection set    set<int> intersect = intersection(s1, s2);     // Size of the intersection set    double size_in = intersect.size();     // Calculate the Jaccard index    // using the formula    double jaccard_in = size_in                        / (size_s1 + size_s2 - size_in);     // Return the Jaccard index    return jaccard_in;} // Function to return the Jaccard distancedouble jaccard_distance(double jaccardIndex){    // Calculate the Jaccard distance    // using the formula    double jaccard_dist = 1 - jaccardIndex;     // Return the Jaccard distance    return jaccard_dist;} // Driver codeint main(){    // Elements of the 1st set    set<int> s1;    s1.insert(1);    s1.insert(2);    s1.insert(3);    s1.insert(4);    s1.insert(5);     // Elements of the 2nd set    set<int> s2;    s2.insert(4);    s2.insert(5);    s2.insert(6);    s2.insert(7);    s2.insert(8);    s2.insert(9);    s2.insert(10);     double jaccardIndex = jaccard_index(s1, s2);     // Print the Jaccard index and Jaccard distance    cout << "Jaccard index = "         << jaccardIndex << endl;    cout << "Jaccard distance = "         << jaccard_distance(jaccardIndex);     return 0;}

Java

 // Java implementation of the approachimport java.util.*;class GFG{   // Function to return the  // intersection set of s1 and s2  static HashSet intersection(HashSet a, HashSet b)  {     // Find the intersection of the two sets    HashSet intersect = new HashSet();    for (int n : a)    {      if (b.contains(n))        intersect.add(n);    }     return intersect;  }   // Function to return the Jaccard index of two sets  static double jaccard_index(HashSet s1, HashSet s2)  {    // Sizes of both the sets    int size_s1 = s1.size();    int size_s2 = s2.size();     // Get the intersection set    HashSet intersect = intersection(s1, s2);     // Size of the intersection set    int size_in = intersect.size();     // Calculate the Jaccard index    // using the formula    double jaccard_in  = (double)size_in / (double)(size_s1 + size_s2 - size_in);     // Return the Jaccard index    return jaccard_in;  }   // Function to return the Jaccard distance  static double jaccard_distance(double jaccardIndex)  {    // Calculate the Jaccard distance    // using the formula    double jaccard_dist = 1 - jaccardIndex;     // Return the Jaccard distance    return jaccard_dist;  }   // Driver code   // Elements of the 1st set  public static void main(String[] args)  {    HashSet s1 = new HashSet();    s1.add(1);    s1.add(2);    s1.add(3);    s1.add(4);    s1.add(5);     // Elements of the 2nd set    HashSet s2 = new HashSet();    s2.add(4);    s2.add(5);    s2.add(6);    s2.add(7);    s2.add(8);    s2.add(9);    s2.add(10);     double jaccardIndex = jaccard_index(s1, s2);     // Print the Jaccard index and Jaccard distance    System.out.println("Jaccard index = " + jaccardIndex);    System.out.println("Jaccard distance = " +                      jaccard_distance(jaccardIndex));  }} // This code is contributed by phasing17

Python3

 # Python3 implementation of the approach  # Function to return the # intersection set of s1 and s2 def intersection(s1, s2) :     # Find the intersection of the two sets     intersect = s1 & s2 ;     return intersect;   # Function to return the Jaccard index of two sets def jaccard_index(s1, s2) :         # Sizes of both the sets     size_s1 = len(s1);     size_s2 = len(s2);      # Get the intersection set     intersect = intersection(s1, s2);      # Size of the intersection set     size_in = len(intersect);      # Calculate the Jaccard index     # using the formula     jaccard_in = size_in  / (size_s1 + size_s2 - size_in);      # Return the Jaccard index     return jaccard_in;   # Function to return the Jaccard distance def jaccard_distance(jaccardIndex)  :     # Calculate the Jaccard distance     # using the formula     jaccard_dist = 1 - jaccardIndex;      # Return the Jaccard distance     return jaccard_dist;   # Driver code if __name__ == "__main__" :      # Elements of the 1st set     s1 = set();     s1.add(1);     s1.add(2);     s1.add(3);     s1.add(4);     s1.add(5);      # Elements of the 2nd set     s2 = set();     s2.add(4);     s2.add(5);     s2.add(6);     s2.add(7);     s2.add(8);     s2.add(9);     s2.add(10);      jaccardIndex = jaccard_index(s1, s2);      # Print the Jaccard index and Jaccard distance     print("Jaccard index = ",jaccardIndex);     print("Jaccard distance = ",jaccard_distance(jaccardIndex));          # This code is contributed by AnkitRai01

C#

 // C# implementation of the approachusing System;using System.Collections.Generic; class GFG{   // Function to return the  // intersection set of s1 and s2  static HashSet<int> intersection(HashSet<int> a, HashSet<int> b)  {     // Find the intersection of the two sets    HashSet<int> intersect = new HashSet<int>();    foreach (int n in a)    {      if (b.Contains(n))        intersect.Add(n);    }     return intersect;  }   // Function to return the Jaccard index of two sets  static double jaccard_index(HashSet<int> s1, HashSet<int> s2)  {    // Sizes of both the sets    int size_s1 = s1.Count;    int size_s2 = s2.Count;     // Get the intersection set    HashSet<int> intersect = intersection(s1, s2);     // Size of the intersection set    int size_in = intersect.Count;     // Calculate the Jaccard index    // using the formula    double jaccard_in  = (double)size_in / (double)(size_s1 + size_s2 - size_in);     // Return the Jaccard index    return jaccard_in;  }   // Function to return the Jaccard distance  static double jaccard_distance(double jaccardIndex)  {    // Calculate the Jaccard distance    // using the formula    double jaccard_dist = 1 - jaccardIndex;     // Return the Jaccard distance    return jaccard_dist;  }   // Driver code   // Elements of the 1st set  public static void Main(string[] args)  {    HashSet<int> s1 = new HashSet<int>();    s1.Add(1);    s1.Add(2);    s1.Add(3);    s1.Add(4);    s1.Add(5);     // Elements of the 2nd set    HashSet<int> s2 = new HashSet<int>();    s2.Add(4);    s2.Add(5);    s2.Add(6);    s2.Add(7);    s2.Add(8);    s2.Add(9);    s2.Add(10);     double jaccardIndex = jaccard_index(s1, s2);     // Print the Jaccard index and Jaccard distance    Console.WriteLine("Jaccard index = " + jaccardIndex);    Console.WriteLine("Jaccard distance = " +                      jaccard_distance(jaccardIndex));  }} // This code is contributed by phasing17

Javascript

 // JavaScript implementation of the approach // Function to return the// intersection set of s1 and s2function intersection(a, b){    // Find the intersection of the two sets    let intersect = new Set([...a].filter(i => b.has(i)));     return intersect;} // Function to return the Jaccard index of two setsfunction jaccard_index(s1, s2){    // Sizes of both the sets    let size_s1 = s1.size;    let size_s2 = s2.size;     // Get the intersection set    let intersect = intersection(s1, s2);     // Size of the intersection set    let size_in = intersect.size;     // Calculate the Jaccard index    // using the formula    let jaccard_in  = size_in / (size_s1 + size_s2 - size_in);     // Return the Jaccard index    return jaccard_in;} // Function to return the Jaccard distancefunction jaccard_distance(jaccardIndex){    // Calculate the Jaccard distance    // using the formula    let jaccard_dist = 1 - jaccardIndex;     // Return the Jaccard distance    return jaccard_dist;} // Driver code // Elements of the 1st setlet s1 = new Set();s1.add(1);s1.add(2);s1.add(3);s1.add(4);s1.add(5); // Elements of the 2nd setlet s2 = new Set();s2.add(4);s2.add(5);s2.add(6);s2.add(7);s2.add(8);s2.add(9);s2.add(10); let jaccardIndex = jaccard_index(s1, s2); // Print the Jaccard index and Jaccard distanceconsole.log("Jaccard index = ", jaccardIndex);console.log("Jaccard distance = ",            jaccard_distance(jaccardIndex)); // This code is contributed by phasing17

Output:
Jaccard index = 0.2
Jaccard distance = 0.8

Previous
Next