Whatever message this page gives is out now! Go check it out!

difference

Last update:
May 18, 2026
Returns elements in this set that are not in the other set.

Description

Finds differences in elements from two sets. Returns elements in a set that are not present in the other set.

Returns

A new Set.

Category

ColdFusion Set member methods

Function syntax

set.difference(otherSet)

Parameters

ParameterDescription
otherSetAnother Set.

See also

Example

The following example uses this API in cfscript (syntax: set.difference(otherSet)).

<cfscript>  
    setA = setNew();
    setA.add(1);
    setA.add(2);
    setA.add(3);

    setB = setNew();
    setB.add(2);
    setB.add(3);
    setB.add(4);

    differenceSet = setA.difference(setB);
    writeDump(differenceSet) // 1
    writeOutput(differenceSet.size()); // 1  (only 1 remains)
</cfscript> 
      

Real-world example

Sync: primary keys in the database but not in the uploaded file are candidates for archival.

<cfscript>
    /**
    * Keys that exist in the database but were not in the latest file → candidates for removal.
    */
    function idsToRemove(required any dbIds, required any fileIds) {
        return dbIds.difference(fileIds);
    }

    dbIds = setNew();
    dbIds.add("a");
    dbIds.add("b");
    dbIds.add("c");

    fileIds = setNew();
    fileIds.add("b");
    fileIds.add("c");

    stale = idsToRemove(dbIds, fileIds);
    writeOutput(stale.has("a")); // true — only "a" is extra in db
</cfscript>
      

Share this page

Was this page helpful?
We're glad. Tell us how this page helped.
We're sorry. Can you tell us what didn't work for you?
Thank you for your feedback. Your response will help improve this page.

On this page