需要找到具有相同的所有交易 sourceAccount
, targetAccount
, category
, amount
,和每个连续交易之间的时间差小于1分钟。
{
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",
},
];
预期产量:
[
[
{
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 findDuplicateTransactions = (transactions = []) => {
// Add your implementation here...
if (transactions.length === 0) {
return [];
}
let result = [];
for (let item of transactions) {
for (let checkingItem of transactions) {
if (
transactions.indexOf(item) !== transactions.indexOf(checkingItem) &&
item.sourceAccount === checkingItem.sourceAccount &&
item.targetAccount === checkingItem.targetAccount &&
item.amount === checkingItem.amount &&
item.category === checkingItem.category
) {
if (result.indexOf(checkingItem) === -1) {
result.push(checkingItem);
}
}
}
}
return result;
};
这是一个非常有效的解决方案。我使用哈希图将项目有效地分组在一起,因此由于排序,此处的时间复杂度为O(nlog(n))。
let input = [ {
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 MILLIS_IN_MINUTE = 1000 * 60;
const hashedItems = {};
function itemToHash(item) {
return `${item.sourceAccount}#${item.targetAccount}#${item.category}#${item.amount}`;
}
input.sort((a, b) => {
const dateComp = new Date(a.time) - new Date(b.time);
return dateComp ? dateComp : a.id - b.id;
});
for (let i=0; i<input.length; i++) {
const item = input[i];
const itemHash = itemToHash(item);
if (!hashedItems[itemHash]) {
hashedItems[itemHash] = [item];
} else {
const last = hashedItems[itemHash][(hashedItems[itemHash].length-1)];
if (new Date(item.time) - new Date(last.time) <= MILLIS_IN_MINUTE) {
hashedItems[itemHash].push(item);
}
}
}
let result = [];
for (let res of Object.values(hashedItems)) {
result.push(res);
}
console.log(result);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句