It is unclear why you would want to use a script here, given that a range can be filtered as requested with a plain vanilla spreadsheet formula, like this:
=arrayformula(
iferror(
vlookup(
unique(A2:A),
sort(A2:D, row(A2:D), false),
column(A2:D),
false
)
)
)
To replace your original data with the unique results, use ControlC to copy and ControlShiftV to paste values only. On a Mac, use ⌘C and ⌘ShiftV.
The question specifies Apps Script, so here's a custom function to do the same, together with a simple function that uses it to filter a hard-coded range in a spreadsheet.
/**
* Removes duplicates in 'Sheet1!A2:D'.
* Keeps each last row where a key appears.
* Other rows are cleared.
*/
function removePriorDuplicates() {
const range = SpreadsheetApp.getActive().getRange('Sheet1!A2:D');
const uniques = getLastUniques(range.getValues(), 0);
range
.clearContent()
.offset(0, 0, uniques.length, uniques[0].length)
.setValues(uniques);
}
/**
* Gets exactly one row per each unique key in a key column.
* Keeps each last row where a key appears.
* Other rows are filtered out.
*
* @param {Object[][]} array2D The array to filter.
* @param {Number} columnIndex The zero-indexed column number where keys appear.
* @return {Object[][]} A 2D array where there is just one copy of each key.
* @customfunction
*/
function getLastUniques(array2D, columnIndex) {
// version 1.0, written by --Hyde, 16 September 2021
// - see https://stackoverflow.com/a/69204946/13045193
const keys = array2D.map(row => row[columnIndex]);
return array2D.filter((row, index) => keys.lastIndexOf(row[columnIndex]) === index);
}