Git Product home page Git Product logo

array-set's Introduction

Arrays as Sets

Micro-implementation of the basic operations in Set Theory applied on Javascript arrays.

Using arrays as logical sets, or as groups of similar elements, is extremely common and often requires the use of set operations. Unfortunately such operations have not found yet place in the current version of ECMAScript (ES5).

Luckily their implementation requires just a few lines of code, not nearly enough to make it worth creating a library or a module.

If you need to use the operations for more than two arrays the following rules apply:

A ∪ B ∪ C = A ∪ (B ∪ C) = (A ∪ B) ∪ C

A ∩ B ∩ C = A ∩ (B ∩ C) = (A ∩ B) ∩ C


Live demo at: jsfiddle.net/swogger/4gpDB/4/


Union

Merging two arrays/sets into one without duplicating elements.

Often merging two or more arrays into one is required. Unfortunately the native implementation Array.prototype.concat() preserves the duplicate values both arrays have in common, which requires more code to be resolved. Union takes care of that. #####Code:

var fruit = ['apple', 'banana', 'lemon', 'lime', 'pear'];
var sweet = ['icecream', 'banana', 'honey', 'pear', 'sugar', 'apple'];
var result = union(fruit, sweet);

#####Preview:

Fruit Set: apple, banana, lemon, lime, pear
Sweet Set: icecream, banana, honey, pear, sugar, apple
Result Set: apple, banana, lemon, lime, pear, icecream, honey, sugar

Difference (Relative Complement)

Remove all elements from array/set 1 that have equivalent in array/set 2.

#####Code:

var fruit = ['apple', 'banana', 'lemon', 'lime', 'pear'];
var sweet = ['icecream', 'banana', 'honey', 'pear', 'sugar', 'apple'];
var result = difference(fruit, sweet);

#####Preview:

Fruit Set: apple, banana, lemon, lime, pear
Sweet Set: icecream, banana, honey, pear, sugar, apple
Result Set: lemon, lime

Intersection

Returns only the elements both arrays/sets have in common.

#####Code:

var fruit = ['apple', 'banana', 'lemon', 'lime', 'pear'];
var sweet = ['icecream', 'banana', 'honey', 'pear', 'sugar', 'apple'];
var result = intersection(fruit, sweet);

#####Preview:

Fruit Set: apple, banana, lemon, lime, pear
Sweet Set: icecream, banana, honey, pear, sugar, apple
Result Set: apple, banana, pear

Exclusion (Symmetric Difference)

Returns the elements from both arrays that they did NOT have in common.

#####Code:

var fruit = ['apple', 'banana', 'lemon', 'lime', 'pear'];
var sweet = ['icecream', 'banana', 'honey', 'pear', 'sugar', 'apple'];
var result = exclusion(fruit, sweet);

#####Preview:

Fruit Set: apple, banana, lemon, lime, pear
Sweet Set: icecream, banana, honey, pear, sugar, apple
Result Set: lemon, lime, icecream, honey, sugar

###Thanks for reading! Feedback always welcome.

array-set's People

Stargazers

 avatar

Watchers

 avatar

array-set's Issues

Duplicates aren't removed from input arrays

For both difference() and intersection(), there is an unmentioned precondition: both must not include duplicates, or there will be duplicates in the resulting array as well:

a = [2,3,3,4]
b = [3,5,6,6]

intersection(a, b) // [3,3,4]
difference(b, a) // [5,6,7,5]

A quick workaround is to a = union(a,a) and b = union(b,b), which will filter out duplicates.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.