Dedupe models and group them by family
A model inventory with repeated entries: keep the latest of each one and count how many there are per family.
RECORDS is a download inventory shaped { name, family, date, digest }. The same name appears several times.
- Write
dedupeLatest(records): return a new array with one record pername, the one with the latestdate, sorted bynameascending. Do not modify the input array. - Write
countByFamily(records): return an object{ family: count }.
Dates use the YYYY-MM-DD format, so they compare correctly as strings.
Challenges 0/5
- Keeps one record per model, sorted by name
- Keeps the most recent entry
- Does not modify the input array
- Counts per family after deduping
- An empty inventory gives [] and {}
function dedupeLatest(records) {
// keep one record per name: the one with the latest date
// then sort by name
return records.slice();
}
function countByFamily(records) {
// { family: count }
return {};
}
console.log(countByFamily(dedupeLatest(RECORDS)));Tab indents, Ctrl+Z undoes. To leave the editor with the keyboard: Esc, then Tab.
Why this matters
Keeping the latest record for each key and counting per group is the most repeated operation in data preparation, and not only for model inventories. It is what you do when you re-index a folder where one document has three versions, or when you clean a CRM before handing it to an assistant. Without deduplication, the RAG retrieves the old version of a policy as confidently as the current one. Leaving the input untouched avoids the classic mistake of sorting the original list by accident. The full case is in local RAG with Qdrant and bge-m3.
Go deeper: the MDN reference →
This in production, with your data? Let's talk for 15 minutes →