Array based implementation

Sample test case at http://jsfiddle.net/ryan_s/n67vc/