JSFiddle - React, Tailwind, and code Playground

by Abhishek Kumar

JavaScript

function findDuplicateTransactions(transactions = []) {
    // return your result from this function
    function compare(a, b) {
        let aTime = new Date(a.time),
            bTime = new Date(b.time);
        if (aTime < bTime) return -1;
        if (aTime > bTime) return 1;
        return 0;
    }

    function isIdentical(a, b) {
        let aTime = new Date(a.time),
            bTime = new Date(b.time),
            aLimit = new Date(+aTime);
        aLimit.setMinutes(aLimit.getMinutes() + 1);
        return (a.sourceAccount == b.sourceAccount &&
            a.targetAccount == b.targetAccount &&
            a.category == b.category &&
            a.amount == b.amount &&
            aTime <= bTime &&
            aLimit > bTime);
    }

    let duplicates = [];
    transactions.sort(compare);
    console.log(transactions);
    for (let i = 0; i < transactions.length; i++) {
        console.log('+', i);
        let matches = [],
            pivot = transactions[i];
        for (let j = 0; j < transactions.length; j++) {
            console.log('-', j);
            let transaction = transactions[j];
            if (isIdentical(pivot, transaction)) {
                console.log('.', transaction.id, transactions.length);
                pivot = transaction;
                matches = matches.concat(transactions.splice(j, 1));
            }
        }
        if (matches.length > 0) {
            duplicates.push(matches);
        }
    }
    return duplicates;
}

var txn = findDuplicateTransactions([
  {
    "id": 3,
    "sourceAccount": "A",
    "targetAccount": "B",
    "amount": 100,
    "category": "eating_out",
    "time": "2018-03-02T10:34:30.000Z"
  },
  {
    "id": 1,
    "sourceAccount": "A",
    "targetAccount": "B",
    "amount": 100,
    "category": "eating_out",
    "time": "2018-03-02T10:33:00.000Z"
  },
  {
    "id": 6,
    "sourceAccount": "A",
    "targetAccount": "C",
    "amount": 250,
    "category": "other",
    "time":...