Just The Code Please

How to check if an Array can be treated like a Set in Javascript

January 16th 2024

Summary

Sets are very useful. Using sets ensure that all values in the collection are unique. Though newer versions of Javascript have Sets built into the language, complete support has not been adopted by all browsers.

It's common to see arrays used like sets in Javascript. This function checks to see if all values in an array are unique and are the same type. If you don't find type checking to be useful, I provided a version without is as well. I've found this to be useful in some of the projects I've worked on, I hope you will too!

The Code

With Type Checking

Javascript
// This implementation is good for arrays of primitive values
function isSet(array){
    if(array.length > 0) {
        const occurrenceCount = {};
        const typeOfObject = typeof array[0];
        for(let a of array) {
            if(typeOfObject !== typeof a) return false;
            if(!occurrenceCount[a]) occurrenceCount[a] = 0;
            occurrenceCount[a] = occurrenceCount[a] + 1;
            if(occurrenceCount[a] > 1) return false;
        }
        return true;
    } else {
        return true;//Empty Set
    }
}

console.log(isSet([1,2,3,4,5,6]));//true
console.log(isSet([1,2,3,4,5,6,6]));//false
console.log(isSet([1,2,3,4,5,"6"]));//false
Javascript
// This implementation is good for arrays of objects
function isSet(array, getIdFunc){
    if(array.length > 0) {
        const occurrenceCount = {};
        const typeOfObject = typeof array[0];
        for(let a of array) {
            if(typeOfObject !== typeof a) return false;
            const id = getIdFunc(a);
            if(!occurrenceCount[id]) occurrenceCount[id] = 0;
            occurrenceCount[id] = occurrenceCount[id] + 1;
            if(occurrenceCount[id] > 1) return false;
        }
        return true;
    } else {
        return true;//Empty Set
    }
}
const a1 = [
    {id: 1,text: "text"},
    {id: 2,text: "other text"},
];
const a2 = [
    {id: 1,text: "text"},
    {id: 2,text: "other text"},
    {id: 1,text: "text"}
];
const a3 = [
    {id: 1,text: "text"},
    {id: 2,text: "other text"},
    {id: 1,text: "text"},
    "not the same type"
];

console.log(isSet(a1,function(o) { return o.id; } ));//true
console.log(isSet(a2,function(o) { return o.id; }));//false
console.log(isSet(a3,function(o) { return o.id; }));//false

Without Type Checking

Javascript
// This implementation is good for arrays of primitive values
function isSet(array){
    if(array.length > 0) {
        const occurrenceCount = {};
        for(let a of array) {
            if(!occurrenceCount[a]) occurrenceCount[a] = 0;
            occurrenceCount[a] = occurrenceCount[a] + 1;
            if(occurrenceCount[a] > 1) return false;
        }
        return true;
    } else {
        return true;//Empty Set
    }
}

console.log(isSet([1,2,3,4,5,6]));//true
console.log(isSet([1,2,3,4,5,6,6]));//false
console.log(isSet([1,2,3,4,5,"6"]));//false