Javascript 在数组中查找重复对象

Javascript 在数组中查找重复对象,javascript,Javascript,需要查找具有相同源帐户,目标帐户,类别,金额的所有交易记录, 并且每个连续事务之间的时间差小于1分钟 { id: 3, sourceAccount: "A", targetAccount: "B", amount: 100, category: "eating_out", time: "2018-03-02T10:34:30.000Z", }, { id: 1, sourceAccount: "A", targe

需要查找具有相同
源帐户
目标帐户
类别
金额
的所有交易记录, 并且每个连续事务之间的时间差小于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;
};

这里有一个非常有效的解决方案。我使用hashmap将项目有效地分组在一起,因此由于排序的原因,这里的时间复杂度是O(nlog(n))

let input=[{
id:3,
sourceAccount:“A”,
目标帐户:“B”,
金额:100,
类别:“外出就餐”,
时间:“2018-03-02T10:34:30.000Z”,
},
{
id:1,
sourceAccount:“A”,
目标帐户:“B”,
金额:100,
类别:“外出就餐”,
时间:“2018-03-02T10:33:00.000Z”,
},
{
id:6,
sourceAccount:“A”,
目标帐户:“C”,
金额:250,
类别:“其他”,
时间:“2018-03-02T10:33:05.000Z”,
},
{
id:4,
sourceAccount:“A”,
目标帐户:“B”,
金额:100,
类别:“外出就餐”,
时间:“2018-03-02T10:36:00.000Z”,
},
{
id:2,
sourceAccount:“A”,
目标帐户:“B”,
金额:100,
类别:“外出就餐”,
时间:“2018-03-02T10:33:50.000Z”,
},
{
id:5,
sourceAccount:“A”,
目标帐户:“C”,
金额:250,
类别:“其他”,
时间:“2018-03-02T10:33:00.000Z”,
},
];
const MILLIS_IN_min=1000*60;
常量hashedItems={};
函数itemToHash(item){
返回`${item.sourceAccount}}${item.targetAccount}`${item.category}`${item.amount}`;
}
输入.排序((a,b)=>{
const dateComp=新日期(a时间)-新日期(b时间);
返回日期comp?日期comp:a.id-b.id;
});

对于(设i=0;i这是一个非常有效的解决方案。我使用hashmap将项目有效地分组在一起,因此由于排序的原因,这里的时间复杂度为O(nlog(n))

let input=[{
id:3,
sourceAccount:“A”,
目标帐户:“B”,
金额:100,
类别:“外出就餐”,
时间:“2018-03-02T10:34:30.000Z”,
},
{
id:1,
sourceAccount:“A”,
目标帐户:“B”,
金额:100,
类别:“外出就餐”,
时间:“2018-03-02T10:33:00.000Z”,
},
{
id:6,
sourceAccount:“A”,
目标帐户:“C”,
金额:250,
类别:“其他”,
时间:“2018-03-02T10:33:05.000Z”,
},
{
id:4,
sourceAccount:“A”,
目标帐户:“B”,
金额:100,
类别:“外出就餐”,
时间:“2018-03-02T10:36:00.000Z”,
},
{
id:2,
sourceAccount:“A”,
目标帐户:“B”,
金额:100,
类别:“外出就餐”,
时间:“2018-03-02T10:33:50.000Z”,
},
{
id:5,
sourceAccount:“A”,
目标帐户:“C”,
金额:250,
类别:“其他”,
时间:“2018-03-02T10:33:00.000Z”,
},
];
const MILLIS_IN_min=1000*60;
常量hashedItems={};
函数itemToHash(item){
返回`${item.sourceAccount}}${item.targetAccount}`${item.category}`${item.amount}`;
}
输入.排序((a,b)=>{
const dateComp=新日期(a时间)-新日期(b时间);
返回日期comp?日期comp:a.id-b.id;
});

for(让i=0;iIt可以通过reduce函数处理。我发布了我的工作解决方案。@YaronSchwimmer家庭作业也许可以。但在我看来,OP在这方面付出了一些努力,问题并没有那么简单。例如,superdev能做的最好的事情就是O(n^3)解决方案,他必须修改3次才能得到正确的结果。但实际上仍然是错误的,因为他没有按id对其进行排序,因此它与输出不匹配,并且有一些边缘案例可能会破坏它。@user120242我撤回了接近票。感谢大家,我花时间以有效的方式解决了这个问题:)@YaronSchwimmer,是的,你是对的,这是实习任务,我自己试了三天,但在reduce中使用循环对我来说并不明显;-)它可以通过reduce函数处理。我发布了我的工作解决方案。@YaronSchwimmer可能是家庭作业。但在我看来,OP在这方面付出了一点努力,问题并不是那么简单。例如,superdev能用它做的最好的解决方案是一个O(n^3)解决方案,他必须修改3次才能正确。事实上,这仍然是错误的,因为他没有按id对它进行排序,所以它与输出不匹配,并且有一些边缘案例可能会破坏它。@user120242我撤回了接近票数的投票。谢谢你,只是为了好玩,我花时间以一种有效的方式解决了这个问题:)@YaronSchwimmer,是的,你说得对,这是一项实习任务,我自己试了三天,但在reduce中使用loop对我来说并不明显;-)
const findDuplicateTransactions = (transactions = []) => {
  // Add your implementation here...
  if (transactions.length === 0) {
    return [];
  }

  return transactions.reduce((result, x) => {
    for (let i = 0; i < result.length; i ++) {
        if (result[i].findIndex(item => item.sourceAccount === x.sourceAccount &&
        item.targetAccount === x.targetAccount &&
        item.amount === x.amount &&
        item.category === x.category &&
        Math.floor(new Date(item.time).getTime() / (1000 * 60)) === Math.floor(new Date(x.time).getTime() / (1000 * 60))) !== -1) {
          result[i].push(x);
          return result;
        }
    }
    result.push([x]);
    return result;
  }, []);
};
[[{
  amount: 100,
  category: "eating_out",
  id: 3,
  sourceAccount: "A",
  targetAccount: "B",
  time: "2018-03-02T10:34:30.000Z"
}], [{
  amount: 100,
  category: "eating_out",
  id: 1,
  sourceAccount: "A",
  targetAccount: "B",
  time: "2018-03-02T10:33:00.000Z"
}, {
  amount: 100,
  category: "eating_out",
  id: 2,
  sourceAccount: "A",
  targetAccount: "B",
  time: "2018-03-02T10:33:50.000Z"
}], [{
  amount: 250,
  category: "other",
  id: 6,
  sourceAccount: "A",
  targetAccount: "C",
  time: "2018-03-02T10:33:05.000Z"
}, {
  amount: 250,
  category: "other",
  id: 5,
  sourceAccount: "A",
  targetAccount: "C",
  time: "2018-03-02T10:33:00.000Z"
}], [{
  amount: 100,
  category: "eating_out",
  id: 4,
  sourceAccount: "A",
  targetAccount: "B",
  time: "2018-03-02T10:36:00.000Z"
}]]