Last active
December 28, 2015 22:19
-
-
Save garbados/7570792 to your computer and use it in GitHub Desktop.
This file contains 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
/* | |
* Given array `records` and function `fn` | |
* returns an array where | |
* any records with duplicate `fn(record)` values | |
* are omitted. | |
* | |
* Runs in O(n) where n is records.length | |
*/ | |
function remove_duplicates (records, fn) { | |
var uniques = {}, | |
results = []; | |
records.forEach(function (record) { | |
var property = fn(record), | |
has_property = uniques[property]; | |
if (!has_property) { | |
// add to results | |
results.push(record); | |
// indicate that we've already seen this value | |
uniques[property] = true; | |
} | |
}); | |
return results; | |
} | |
// for example | |
var records = [...] | |
records = remove_duplicates(records, function (record) { | |
return record.rawDataChecksum; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment