Skip to content

Instantly share code, notes, and snippets.

@ox
Created October 1, 2013 05:23
Show Gist options
  • Save ox/6774191 to your computer and use it in GitHub Desktop.
Save ox/6774191 to your computer and use it in GitHub Desktop.
Take an array of numbers and returns an array of groups of identical numbers. [2, 1, 2] would return [[1], [2, 2]].
var k = [1,4,2,5,3,2,2,5,7,8,8,5,3,4,4,4,2,1]
function bundle(arr) {
var t = {}
arr.map(function (x) {
if (!t[x]) t[x] = []
t[x].push(x)
})
return Object.keys(t).reduce(function (memo, x) { return memo.concat([t[x]])}, [])
}
console.log(bundle(k))
@thebyrd
Copy link

thebyrd commented Oct 1, 2013

Nasty

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment