Skip to content

Computes the difference between two JavaScript arrays by finding the longest common subsequence between them.

License

Notifications You must be signed in to change notification settings

coditect/array-diff

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ArrayDiff

ArrayDiff computes the difference between two JavaScript arrays (as well as any other object that uses numeric subscripts, such as a string or DOMNodeList) by finding the longest common subsequence between them.

Example

var thisArray = 'nematode knowledge'.split(''),
    thatArray = 'empty bottle'.split(''),
    arrayDiff = new ArrayDiff(thatArray, thisArray);

arrayDiff.diff.forEach(function(element) {

    if (element.added()) {
        var operation = '+'
    } else if (element.removed()) {
        var operation = '-';
    } else {
        var operation = ' ';
    }
    
    console.log('%s %s', operation, element.item);
    
});

Acknowledgements

Many thanks to Professor David Eppstein of the University of California, Irvine, whose lecture notes from February 1996 helped me get my head around the dynamic programming solution to the longest common subsequence problem.

About

Computes the difference between two JavaScript arrays by finding the longest common subsequence between them.

Resources

License

Stars

Watchers

Forks

Packages

No packages published