Open In App

D3.js | d3.set.size() Function

The set.size() function in D3.js is used to count the number of unique elements and return the number of unique elements in the given array.

Syntax:



d3.set([Array]).size();

Parameters: This function accepts a parameter Array of strings.

Return Value: It returns the number of unique elements from the given array.



Below programs illustrate the d3.set.size() function in D3.js:

Example 1:




<!DOCTYPE html>
<html>
   
<head>
    <title>d3.set.size() Function</title>
       
    <script src='https://d3js.org/d3.v4.min.js'></script>
</head>
<body>
    
  <script>
      
     // Initialising an array
     Array1 = ["a", "a", "b", "c"];
     Array2 = ["c", "c", "c"];
     Array3 = ["Geeks", "gfg", "GeeksforGeeks"];
       
     // Calling the d3.set.size() function
     A = d3.set(Array1).size();
     B = d3.set(Array2).size();
     C = d3.set(Array3).size();
       
     // Getting the number of unique values in the array
     console.log(A);
     console.log(B);
     console.log(C);
  </script>
</body>

Output:

3
1
3

Example 2:




<!DOCTYPE html>
<html>
   
<head>
    <title>d3.set.size() Function</title>
       
    <script src='https://d3js.org/d3.v4.min.js'></script>
</head>
<body>
    
  <script>
       
     // Calling the d3.set.size() function
     // with a array parameter
     A = d3.set([1, 2, 3, 3]).size();
     B = d3.set(["a"]).size();
     C = d3.set(["a", "b", "c", "a", "b", "c"]).size();
       
     // Getting the number of unique values in the array
     console.log(A);
     console.log(B);
     console.log(C);
  </script>
</body>

Output:

3
1
3

Ref: https://devdocs.io/d3~5/d3-collection#set_size


Article Tags :