Created
March 30, 2021 07:06
-
-
Save anilpai/9deb15057ca1e77956cc59ba977f7782 to your computer and use it in GitHub Desktop.
Find Duplicate transactions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function findDuplicateTransactions (transactions = []) { | |
| transactions.sort((a, b) => new Date(a.time) - new Date(b.time)); | |
| let duplicates = [] | |
| let match = [] | |
| let src, dst; | |
| while(transactions.length > 1){ | |
| src = 0 | |
| dst = 1 | |
| match = [src] | |
| while ((new Date(transactions[dst].time) - new Date(transactions[src].time)) <= 60000 && src < transactions.length -1){ | |
| if(compare_equal(transactions[src], transactions[dst])){ | |
| match.push(dst) | |
| src = dst | |
| } | |
| dst += 1 | |
| if (dst === transactions.length){ | |
| break | |
| } | |
| } | |
| if (match.length > 1){ | |
| let temp = [] | |
| while(match.length){ | |
| let item = transactions.splice(match.pop(), 1) | |
| temp.unshift(item[0]) | |
| } | |
| duplicates.push(temp) | |
| match = [] | |
| } else{ | |
| transactions.shift() | |
| } | |
| if (transactions.length == 1){ | |
| break; | |
| } | |
| } | |
| return duplicates | |
| } | |
| function compare_equal(t1,t2){ | |
| if (t1.sourceAccount === t2.sourceAccount && t1.targetAccount === t2.targetAccount && t1.amount === t2.amount && t1.category === t2.category){ | |
| return true; | |
| }else{ | |
| return false; | |
| } | |
| } | |
| const transactions = [ | |
| { | |
| 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: '2018-03-02T10:33:05.000Z' | |
| }, | |
| { | |
| id: 4, | |
| sourceAccount: 'A', | |
| targetAccount: 'B', | |
| amount: 100, | |
| category: 'eating_out', | |
| time: '2018-03-02T10:36:00.000Z' | |
| }, | |
| { | |
| id: 2, | |
| sourceAccount: 'A', | |
| targetAccount: 'B', | |
| amount: 100, | |
| category: 'eating_out', | |
| time: '2018-03-02T10:33:50.000Z' | |
| }, | |
| { | |
| id: 5, | |
| sourceAccount: 'A', | |
| targetAccount: 'C', | |
| amount: 250, | |
| category: 'other', | |
| time: '2018-03-02T10:33:00.000Z' | |
| } | |
| ]; | |
| const duplicates = [ | |
| [ | |
| { | |
| id: 1, | |
| sourceAccount: "A", | |
| targetAccount: "B", | |
| amount: 100, | |
| category: "eating_out", | |
| time: "2018-03-02T10:33:00.000Z" | |
| }, | |
| { | |
| id: 2, | |
| sourceAccount: "A", | |
| targetAccount: "B", | |
| amount: 100, | |
| category: "eating_out", | |
| time: "2018-03-02T10:33:50.000Z" | |
| }, | |
| { | |
| id: 3, | |
| sourceAccount: "A", | |
| targetAccount: "B", | |
| amount: 100, | |
| category: "eating_out", | |
| time: "2018-03-02T10:34:30.000Z" | |
| } | |
| ], | |
| [ | |
| { | |
| id: 5, | |
| sourceAccount: "A", | |
| targetAccount: "C", | |
| amount: 250, | |
| category: "other", | |
| time: "2018-03-02T10:33:00.000Z" | |
| }, | |
| { | |
| id: 6, | |
| sourceAccount: "A", | |
| targetAccount: "C", | |
| amount: 250, | |
| category: "other", | |
| time: "2018-03-02T10:33:05.000Z" | |
| } | |
| ] | |
| ]; | |
| const transactions2 = [ { id: 6, | |
| sourceAccount: 'my_account', | |
| targetAccount: 'internet_shop', | |
| amount: -250, | |
| category: 'other', | |
| time: '2018-03-01T22:16:40.000Z' }, | |
| { id: 201, | |
| sourceAccount: 'company_x', | |
| targetAccount: 'my_account', | |
| amount: 10000, | |
| category: 'pension_benefits', | |
| time: '2018-02-25T08:00:00.000Z' }, | |
| { id: 22, | |
| sourceAccount: 'my_account', | |
| targetAccount: 'restaurant', | |
| amount: -970, | |
| category: 'eating_out', | |
| time: '2018-04-17T19:52:46.000Z' }, | |
| { id: 101, | |
| sourceAccount: 'company_x', | |
| targetAccount: 'my_account', | |
| amount: 240, | |
| category: 'salary', | |
| time: '2018-02-25T08:00:30.000Z' }, | |
| { id: 35, | |
| sourceAccount: 'my_account', | |
| targetAccount: 'coffee_shop', | |
| amount: -90, | |
| category: 'eating_out', | |
| time: '2018-05-07T09:58:06.000Z' }, | |
| { id: 24, | |
| sourceAccount: 'my_account', | |
| targetAccount: 'fitness_club', | |
| amount: -610, | |
| category: 'other', | |
| time: '2018-04-22T11:54:10.000Z' }, | |
| { id: 3, | |
| sourceAccount: 'my_account', | |
| targetAccount: 'supermarket', | |
| amount: -1000, | |
| category: 'groceries', | |
| time: '2018-03-01T17:28:32.000Z' }, | |
| { id: 37, | |
| sourceAccount: 'my_account', | |
| targetAccount: 'supermarket', | |
| amount: -1690, | |
| category: 'groceries', | |
| time: '2018-05-10T18:14:10.000Z' }, | |
| { id: 32, | |
| sourceAccount: 'my_account', | |
| targetAccount: 'coffee_shop', | |
| amount: -90, | |
| category: 'eating_out', | |
| time: '2018-05-07T09:56:09.000Z' }, | |
| { id: 12, | |
| sourceAccount: 'my_account', | |
| targetAccount: 'bowling_place', | |
| amount: -600, | |
| category: 'other', | |
| time: '2018-03-05T21:12:10.000Z' }, | |
| { id: 11, | |
| sourceAccount: 'my_account', | |
| targetAccount: 'supermarket', | |
| amount: -1540, | |
| category: 'groceries', | |
| time: '2018-03-05T16:24:31.000Z' }, | |
| { id: 14, | |
| sourceAccount: 'my_account', | |
| targetAccount: 'coffee_shop', | |
| amount: -50, | |
| category: 'eating_out', | |
| time: '2018-04-01T10:24:40.000Z' }, | |
| { id: 18, | |
| sourceAccount: 'my_account', | |
| targetAccount: 'cinema', | |
| amount: -580, | |
| category: 'other', | |
| time: '2018-04-05T20:01:18.000Z' }, | |
| { id: 40, | |
| sourceAccount: 'my_account', | |
| targetAccount: 'fitness_club', | |
| amount: -610, | |
| category: 'other', | |
| time: '2018-05-22T11:54:10.000Z' }, | |
| { id: 9, | |
| sourceAccount: 'my_account', | |
| targetAccount: 'coffee_shop', | |
| amount: -50, | |
| category: 'eating_out', | |
| time: '2018-03-04T07:14:20.000Z' }, | |
| { id: 4, | |
| sourceAccount: 'my_account', | |
| targetAccount: 'cinema', | |
| amount: -330, | |
| category: 'other', | |
| time: '2018-03-01T20:10:15.000Z' }, | |
| { id: 28, | |
| sourceAccount: 'my_account', | |
| targetAccount: 'supermarket', | |
| amount: -1870, | |
| category: 'groceries', | |
| time: '2018-05-05T10:24:30.000Z' }, | |
| { id: 15, | |
| sourceAccount: 'my_account', | |
| targetAccount: 'coffee_shop', | |
| amount: -50, | |
| category: 'eating_out', | |
| time: '2018-04-01T10:25:10.000Z' }, | |
| { id: 25, | |
| sourceAccount: 'my_account', | |
| targetAccount: 'supermarket', | |
| amount: -850, | |
| category: 'groceries', | |
| time: '2018-04-20T18:51:31.000Z' }, | |
| { id: 31, | |
| sourceAccount: 'my_account', | |
| targetAccount: 'coffee_shop', | |
| amount: -90, | |
| category: 'eating_out', | |
| time: '2018-05-07T09:55:10.000Z' }, | |
| { id: 102, | |
| sourceAccount: 'my_account', | |
| targetAccount: 'internet_shop', | |
| amount: -250, | |
| category: 'other', | |
| time: '2018-03-01T22:16:50.000Z' }, | |
| { id: 19, | |
| sourceAccount: 'my_account', | |
| targetAccount: 'coffee_shop', | |
| amount: -90, | |
| category: 'eating_out', | |
| time: '2018-04-07T09:54:21.000Z' }, | |
| { id: 42, | |
| sourceAccount: 'my_account', | |
| targetAccount: 'cinema', | |
| amount: -450, | |
| category: 'other', | |
| time: '2018-05-23T19:13:10.000Z' }, | |
| { id: 30, | |
| sourceAccount: 'my_account', | |
| targetAccount: 'coffee_shop', | |
| amount: -90, | |
| category: 'eating_out', | |
| time: '2018-05-07T09:54:21.000Z' }, | |
| { id: 36, | |
| sourceAccount: 'my_account', | |
| targetAccount: 'internet_shop', | |
| amount: -1650, | |
| category: 'other', | |
| time: '2018-05-08T21:36:41.000Z' }, | |
| { id: 5, | |
| sourceAccount: 'my_account', | |
| targetAccount: 'coffee_shop', | |
| amount: -50, | |
| category: 'eating_out', | |
| time: '2018-03-02T09:25:20.000Z' }, | |
| { id: 20, | |
| sourceAccount: 'my_account', | |
| targetAccount: 'internet_shop', | |
| amount: -1650, | |
| category: 'other', | |
| time: '2018-04-08T21:36:41.000Z' }, | |
| { id: 41, | |
| sourceAccount: 'my_account', | |
| targetAccount: 'supermarket', | |
| amount: -850, | |
| category: 'groceries', | |
| time: '2018-05-20T18:51:31.000Z' }, | |
| { id: 39, | |
| sourceAccount: 'my_account', | |
| targetAccount: 'coffee_shop', | |
| amount: -70, | |
| category: 'eating_out', | |
| time: '2018-05-15T09:12:20.000Z' }, | |
| { id: 17, | |
| sourceAccount: 'my_account', | |
| targetAccount: 'supermarket', | |
| amount: -1870, | |
| category: 'groceries', | |
| time: '2018-04-05T10:24:30.000Z' }, | |
| { id: 7, | |
| sourceAccount: 'my_account', | |
| targetAccount: 'supermarket', | |
| amount: -160, | |
| category: 'groceries', | |
| time: '2018-03-02T13:14:00.000Z' }, | |
| { id: 27, | |
| sourceAccount: 'company_x', | |
| targetAccount: 'my_account', | |
| amount: 10000, | |
| category: 'salary', | |
| time: '2018-04-25T08:00:00.000Z' }, | |
| { id: 29, | |
| sourceAccount: 'my_account', | |
| targetAccount: 'cinema', | |
| amount: -580, | |
| category: 'other', | |
| time: '2018-05-05T20:01:18.000Z' }, | |
| { id: 38, | |
| sourceAccount: 'my_account', | |
| targetAccount: 'restaurant', | |
| amount: -970, | |
| category: 'eating_out', | |
| time: '2018-05-17T19:52:46.000Z' }, | |
| { id: 21, | |
| sourceAccount: 'my_account', | |
| targetAccount: 'supermarket', | |
| amount: -1690, | |
| category: 'groceries', | |
| time: '2018-04-10T18:14:10.000Z' }, | |
| { id: 10, | |
| sourceAccount: 'my_account', | |
| targetAccount: 'fitness_club', | |
| amount: -560, | |
| category: 'other', | |
| time: '2018-03-04T12:54:10.000Z' }, | |
| { id: 26, | |
| sourceAccount: 'my_account', | |
| targetAccount: 'cinema', | |
| amount: -450, | |
| category: 'other', | |
| time: '2018-04-23T19:13:10.000Z' }, | |
| { id: 8, | |
| sourceAccount: 'my_account', | |
| targetAccount: 'restaurant', | |
| amount: -670, | |
| category: 'eating_out', | |
| time: '2018-03-02T18:54:45.000Z' }, | |
| { id: 13, | |
| sourceAccount: 'my_account', | |
| targetAccount: 'coffee_shop', | |
| amount: -50, | |
| category: 'eating_out', | |
| time: '2018-04-01T10:24:00.000Z' }, | |
| { id: 33, | |
| sourceAccount: 'my_account', | |
| targetAccount: 'coffee_shop', | |
| amount: -90, | |
| category: 'eating_out', | |
| time: '2018-05-07T09:57:05.000Z' }, | |
| { id: 1, | |
| sourceAccount: 'company_x', | |
| targetAccount: 'my_account', | |
| amount: 10000, | |
| category: 'salary', | |
| time: '2018-02-25T08:00:00.000Z' }, | |
| { id: 23, | |
| sourceAccount: 'my_account', | |
| targetAccount: 'coffee_shop', | |
| amount: -70, | |
| category: 'eating_out', | |
| time: '2018-04-15T09:12:20.000Z' }, | |
| { id: 2, | |
| sourceAccount: 'my_account', | |
| targetAccount: 'coffee_shop', | |
| amount: -50, | |
| category: 'eating_out', | |
| time: '2018-03-01T12:34:00.000Z' }, | |
| { id: 16, | |
| sourceAccount: 'company_x', | |
| targetAccount: 'my_account', | |
| amount: 10000, | |
| category: 'salary', | |
| time: '2018-03-25T08:10:00.000Z' } ] | |
| const duplicates2 = [ [ { id: 6, | |
| sourceAccount: 'my_account', | |
| targetAccount: 'internet_shop', | |
| amount: -250, | |
| category: 'other', | |
| time: '2018-03-01T22:16:40.000Z' }, | |
| { id: 102, | |
| sourceAccount: 'my_account', | |
| targetAccount: 'internet_shop', | |
| amount: -250, | |
| category: 'other', | |
| time: '2018-03-01T22:16:50.000Z' } ], | |
| [ { id: 13, | |
| sourceAccount: 'my_account', | |
| targetAccount: 'coffee_shop', | |
| amount: -50, | |
| category: 'eating_out', | |
| time: '2018-04-01T10:24:00.000Z' }, | |
| { id: 14, | |
| sourceAccount: 'my_account', | |
| targetAccount: 'coffee_shop', | |
| amount: -50, | |
| category: 'eating_out', | |
| time: '2018-04-01T10:24:40.000Z' }, | |
| { id: 15, | |
| sourceAccount: 'my_account', | |
| targetAccount: 'coffee_shop', | |
| amount: -50, | |
| category: 'eating_out', | |
| time: '2018-04-01T10:25:10.000Z' } ], | |
| [ { id: 30, | |
| sourceAccount: 'my_account', | |
| targetAccount: 'coffee_shop', | |
| amount: -90, | |
| category: 'eating_out', | |
| time: '2018-05-07T09:54:21.000Z' }, | |
| { id: 31, | |
| sourceAccount: 'my_account', | |
| targetAccount: 'coffee_shop', | |
| amount: -90, | |
| category: 'eating_out', | |
| time: '2018-05-07T09:55:10.000Z' }, | |
| { id: 32, | |
| sourceAccount: 'my_account', | |
| targetAccount: 'coffee_shop', | |
| amount: -90, | |
| category: 'eating_out', | |
| time: '2018-05-07T09:56:09.000Z' }, | |
| { id: 33, | |
| sourceAccount: 'my_account', | |
| targetAccount: 'coffee_shop', | |
| amount: -90, | |
| category: 'eating_out', | |
| time: '2018-05-07T09:57:05.000Z' } ] ] | |
| const transactions3 = [ | |
| { | |
| id: 3, | |
| sourceAccount: 'A', | |
| targetAccount: 'B', | |
| amount: 100, | |
| category: 'eating_out', | |
| time: '2018-03-02T10:34:30.000Z' | |
| }, | |
| { | |
| id: 7, | |
| sourceAccount: 'A', | |
| targetAccount: 'C', | |
| amount: 250, | |
| category: 'coffee_shop', | |
| 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: 'coffee_shop', | |
| time: '2018-03-02T10:34:05.000Z' | |
| }, | |
| { | |
| id: 4, | |
| sourceAccount: 'A', | |
| targetAccount: 'B', | |
| amount: 100, | |
| category: 'eating_out', | |
| time: '2018-03-02T10:36:00.000Z' | |
| }, | |
| { | |
| id: 2, | |
| sourceAccount: 'A', | |
| targetAccount: 'C', | |
| amount: 250, | |
| category: 'eating_out', | |
| time: '2018-03-02T10:33:50.000Z' | |
| }, | |
| { | |
| id: 5, | |
| sourceAccount: 'A', | |
| targetAccount: 'C', | |
| amount: 250, | |
| category: 'coffee_shop', | |
| time: '2018-03-02T10:35:00.000Z' | |
| } | |
| ]; | |
| console.log(findDuplicateTransactions(transactions3)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment