From cf41613361b6c70732a703f06a248e92fdbafe34 Mon Sep 17 00:00:00 2001 From: Vinay Vyas <69166360+vinay-google@users.noreply.github.com> Date: Mon, 13 Feb 2023 10:32:41 -0800 Subject: [PATCH] fix: Update Removing Duplicates to use object for uniqueness check (#385) --- .../removingDuplicates/removingDuplicates.gs | 25 +++++++------------ 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/sheets/removingDuplicates/removingDuplicates.gs b/sheets/removingDuplicates/removingDuplicates.gs index 4047564ee..384ad55f7 100644 --- a/sheets/removingDuplicates/removingDuplicates.gs +++ b/sheets/removingDuplicates/removingDuplicates.gs @@ -20,28 +20,21 @@ */ function removeDuplicates() { // [START apps_script_sheets_sheet] - let sheet = SpreadsheetApp.getActiveSheet(); - let data = sheet.getDataRange().getValues(); + const sheet = SpreadsheetApp.getActiveSheet(); + const data = sheet.getDataRange().getValues(); // [END apps_script_sheets_sheet] - // [START apps_script_sheets_new_data] - let newData = []; - // [END apps_script_sheets_new_data] - for (let i in data) { - let row = data[i]; - let duplicate = false; - for (let j in newData) { - if (row.join() == newData[j].join()) { - duplicate = true; - } - } + const uniqueData = {}; + for (let row of data) { + const key = row.join(); // [START apps_script_sheets_duplicate] - if (!duplicate) { - newData.push(row); - } + uniqueData[key] = uniqueData[key] || row; // [END apps_script_sheets_duplicate] } // [START apps_script_sheets_clear] sheet.clearContents(); + // [START apps_script_sheets_new_data] + const newData = Object.values(uniqueData); + // [END apps_script_sheets_new_data] sheet.getRange(1, 1, newData.length, newData[0].length).setValues(newData); // [END apps_script_sheets_clear] }