Skip to content

Instantly share code, notes, and snippets.

@garbados
Last active December 28, 2015 22:19
Show Gist options
  • Save garbados/7570792 to your computer and use it in GitHub Desktop.
Save garbados/7570792 to your computer and use it in GitHub Desktop.
/*
* 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