在数组中查找重复的对象

亚历克斯·巴西蒂(Alex Basystyi)

需要找到具有相同的所有交易 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;
};
亚伦·史威默(Yaron Schwimmer)

这是一个非常有效的解决方案。我使用哈希图将项目有效地分组在一起,因此由于排序,此处的时间复杂度为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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Swift:在数组中查找重复的CNContact对象

来自分类Dev

在数组中查找重复的对象,并以重复的数目作为新属性返回对象的新数组

来自分类Dev

在数组中查找重复项

来自分类Dev

在数组中查找重复项

来自分类Dev

在数组Ruby中查找重复项

来自分类Dev

在数组中查找重复项

来自分类Dev

在数组中查找重复的整数

来自分类Dev

在数组中查找重复项

来自分类Dev

在数组中查找重复的字母 (javascript)

来自分类Dev

在数组中查找重复的对象值并将其合并-JAVASCRIPT

来自分类Dev

在数组中查找唯一的对象

来自分类Dev

了解在数组中查找对象

来自分类Dev

在数组中查找对象

来自分类Dev

在数组中查找重复和唯一值,然后在对象中将字符串转换为数组

来自分类Dev

在数组中查找非重复元素

来自分类Dev

在数组中查找重复的三合会

来自分类Dev

如何在数组中查找部分重复项?

来自分类Dev

使用Swift在数组中查找重复元素

来自分类Dev

如何在数组中查找和写下重复的元素?

来自分类Dev

在数组/整数列表中查找重复项

来自分类Dev

在数组中查找重复值并将其全部输出

来自分类Dev

在数组中查找最重复数的模

来自分类Dev

如何在数组中查找部分重复项?

来自分类Dev

在数组中查找重复值并将其全部输出

来自分类Dev

在数组/整数列表中查找重复项

来自分类Dev

在数组中查找重复字符串

来自分类Dev

在数组中查找重复连续值的最短方法

来自分类Dev

在数组中查找特定重复数字的索引

来自分类Dev

在数组中查找重复项并打印它们