Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

php
communitycreator

What is the Ds\Set diff() function in PHP?

Kainat Asif

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The Ds\Set::diff() function in PHP is used to find the difference of two sets.

The difference between the two sets is a set that contains the elements of the first set that are not an element of the second set.

Syntax

The Ds\Set::diff() function can be declared as shown in the code snippet below:

Ds\Set public Ds\Set::diff ( Ds\Set $set )

Parameter

The Ds\Set::diff() takes the following parameter:

  • set: The set whose elements will be excluded from the first set.

Return value

The Ds\Set::diff() function returns the difference of the sets, that is, a set containing the elements of the first set that are not an element of the second set.

Example

The code snippet below demonstrates the use of the Ds\Set::diff() function:

<?php
$set1 = new \Ds\Set([1, 2, 3, 4]);
$set2 = new \Ds\Set([3, 4, 5, 6]);
echo("Difference of set1 and set2: \n");
print_r($set1->diff($set2));
?>
Displaying the difference between two sets

Explanation

The following is the explanation of the above code:

  • Line 3: We first declare the set set1.

  • Line 5: We declare the second set, set2.

  • Line 9: We use the Ds\Set::diff() function to find the difference between set1 and set2.

RELATED TAGS

php
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring