Dromo WebinarLearn how Dromo can solve your data importing problems

Register now

Removing Duplicates

Eliminating duplicate entries from your dataset.

Definition

Removing duplicates involves eliminating duplicate entries from your dataset. Duplicates can cause problems in data analysis and produce inaccurate results. Thus, it is often necessary to remove duplicates to ensure data integrity.

Example of removing duplicates using JavaScript

Here is a simple JavaScript array of objects representing a dataset:

const data = [
  { firstName: "John", lastName: "Doe", age: "30" },
  { firstName: "Jane", lastName: "Smith", age: "25" },
  { firstName: "Bob", lastName: "Johnson", age: "40" },
  { firstName: "John", lastName: "Doe", age: "30" },
];

In the above array, we have a duplicate record of "John Doe". To remove this, we can use the .filter() method in combination with the .map() and .indexOf() functions to check for duplicates:

const uniqueData = data.filter(
  (value, index, self) =>
    self
      .map((item) => item.firstName + item.lastName + item.age)
      .indexOf(value.firstName + value.lastName + value.age) === index
);

Here's what is happening:

  1. filter() function: Array.filter() is a method that creates a new array with all elements that pass the test implemented by the provided function. In this case, the function will check if each element in data is a duplicate or not. The function will include the element in uniqueData only if it's not a duplicate.
  2. Inside the filter function: (value, index, self) => ... is the function that is passed into filter(). This function will be applied to every element in data.
    • value represents the current element being processed in the array.
    • index is the index of the current element being processed in the array.
    • self is a reference to the original array (data in this case).
  3. map() function: Array.map() is a method that creates a new array populated with the results of calling a provided function on every element in the calling array. Here, the function item => item.firstName + item.lastName + item.age is applied to every element in self (which is data). This function will return a new array of combined strings (firstName + lastName + age) from each element.
  4. indexOf() function: Array.indexOf() is a method that returns the first index at which a given element can be found in the array, or -1 if it is not present. Here, indexOf(value.firstName + value.lastName + value.age) is looking for the first occurrence of the combined string (firstName + lastName + age) from the value in the array generated by map(). If there are duplicate records, their combined string will have the same value.
  5. Equality Check: ... === index checks if the index of the first occurrence of value‘s combined string (as found by indexOf()) is the same as the current value‘s index in data. If they are the same, it means that this is the first occurrence of this value in the array and therefore it's not a duplicate. If they are different, it means that this value has appeared before in the array (therefore it is a duplicate), and it won't be included in uniqueData.

Before

firstNamelastNameage
JohnDoe30
JaneSmith25
BobJohnson40
JohnDoe30

After

firstNamelastNameage
JohnDoe30
JaneSmith25
BobJohnson40

Considerations

When removing duplicates, it's essential to define what constitutes a duplicate in the context of your data. For instance, in the above example, we considered a record a duplicate if all fields were identical. However, in some cases, you might want to remove duplicates based on a specific field.

  • Concatenating Fields: This operation combines two or more fields into a single field. This could inadvertently create duplicates if not handled correctly.
  • Splitting Fields: This operation separates a single field into multiple fields. After splitting, ensure to check for duplicates that might have been created.
  • Normalizing Cases: Before removing duplicates, you may need to normalize cases to ensure you're not retaining duplicates due to case differences.