Last active
August 29, 2015 14:17
-
-
Save aguestuser/1db63dc58161a9654667 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var _ = require('underscore'); | |
// getDupeList([Tweet]) -> { String : [Int] } | |
// runs in O(n+m) where n is # tweets, m is # dupes | |
function getDupeList(ts){ | |
var tts =_.chain(ts) | |
.map(function(t,i){ return _.extend(t, { i: i });}) | |
.groupBy(function(tt){ return tt.id_str;}) | |
.value(); | |
return _.chain(tts) | |
.keys() | |
.filter(function(k){ return tts[k].length > 1; }) | |
.map(function(kk){ | |
return [kk, _.map(tts[kk], function(tt){ | |
return tt.i;})];}) | |
.object() | |
.value(); | |
} | |
//test getDupeList | |
(function(){ | |
var tweets = [ | |
{ _id: 1, id_str: "123"}, | |
{ _id: 2, id_str: "123"}, | |
{ _id: 3, id_str: "456"}, | |
{ _id: 4, id_str: "123"}, | |
{ _id: 5, id_str: "456"}, | |
{ _id: 6, id_str: "789"} | |
]; | |
var expected = { | |
"123": [0,1,3], | |
"456": [2,4] | |
}; | |
var actual = getDupeList(tweets); | |
if (_.isEqual(actual,expected)){ console.log("PASSING: getDupeList()"); } | |
else { | |
console.log("FAILED: getDupeList()"); | |
console.log("-- expected: ", expected); | |
console.log("-- actual: ", actual); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment