Deduplicate an array of strings or numbers

To remove duplicates in an array of primitive values, such as string or number, you can convert it into a Set and convert it back into an array:

// ๐Ÿ‘‡ Deduplicates an array of primitive values
function deduplicate<T>(array: T[]) {
  return Array.from(new Set(array).values());
}
A function to deduplicate an array using Set in TypeScript.

This works because the Set can only contain unique values. However, it will not work with more complex values such as objects.

Remove duplicates from an array of objects

To deduplicate an array of objects in JavaScript, you should have a way to get a unique string for each item, such as an id. Then you can store those ids in an object, as you filter the array, and if there's already an id stored, skip the element.

Here's a function to deduplicate an array of objects:

// ๐Ÿ‘‡ Deduplicates array of object values. Determines uniqueness by comparing keys
function deduplicate<T>(array: T[], getKey: (item: T) => string | number) {
  const seenItems: Record<string, boolean> = {};
  return array.filter((item) => {
    const key = getKey(item);
    if (seenItems[key]) {
      return false;
    }
    seenItems[key] = true;
    return true;
  });
}
A function to remove duplicates from an array of objects in TypeScript.

The function accepts a getKey function to get a unique string key for each array item. That key will then be used to check uniqueness.

๐Ÿ“Œ
This memorization technique where we store keys in an object makes the function very efficient because we only need to loop through the array once.

Here's how to use it:

deduplicate(items, (item) => item.id));
Deduplicate an array of objects identified by id.

Find this and more code examples in my GitHub repository.