在Array中查找重复数组 [英] Find Duplicate Array within Array

查看:67
本文介绍了在Array中查找重复数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个数组数组,识别重复项的有效方法是什么?

Given an array of arrays, what would be the efficient way of identifying the duplicate item?

var array = [
  [
    11.31866455078125,
    44.53836644772605
  ],
  [                     // <-- Here's the duplicate
    11.31866455078125,
    44.53836644772605
  ],
  [
    11.371536254882812,
    44.53836644772605
  ],
  [
    11.371536254882812,
    44.50140292110874
  ]
]

我一直在用 lodash 作为已接受的依赖项,我得到了如何使用 _.uniqWith _。isEqual

I've been working on this with lodash as an accepted dependency, and I get how to just return the "unique" list using _.uniqWith and _.isEqual:

_.uniqWith(array,_.isEqual)

列表的唯一版本:

[ 
    [ 11.31866455078125,  44.53836644772605 ],
    [ 11.371536254882812, 44.53836644772605 ],
    [ 11.371536254882812, 44.50140292110874 ]
]

但不是只报告唯一元素,我只需要复制的元素,理想情况下是第一次出现的索引。

But rather than just reporting the unique elements, I need just the element that is duplicated, and ideally the index of the first occurrence.

这实际上是否包含在 lodash 库通过一些我缺少的方法组合?或者我只需要编写循环来比较元素。

Is this actually covered in the lodash library by some combination of methods that I'm missing? Or am I just going to have to live with writing loops to compare elements.

可能只是在这方面过度了,所以对这个问题的新观点是值得欢迎的。

Probably just overtired on this, so fresh eyes on the problem would be welcome.

如果有适合的库方法,请尽量不重写函数,所以我基本上坚持:

Trying not to rewrite functions if there are library methods that suit, so I basically am stuck with:


  1. 仅返回重复或至少与唯一列表的比较差异。

  1. Returning just the duplicate or at least the comparison difference with the "unique list".

基本上标识索引数组中的数组。虽然我认为可以通过 _。isEqual <来减少过滤器/ a>一旦识别出重复的项目。

Basically identifying the "index of" an array within an array. Though I suppose that can be a filter reduction with _.isEqual once the duplicate item is identified.

还要避免创建对象哈希/映射并计算这里也出现了键,或者至少不是作为单独的对象,以及可以在功能上在线完成的事情。

Trying also to avoid creating an object Hash/Map and counting the occurrences of keys here as well, or at least not as a separate object, and as something that can be done functionally "in-line".

推荐答案

Lodash提供了许多有用的功能来实现查找第一个重复索引。

使用
_。findIndex() _.isEqual()以下代码将找到第一个重复索引:

Lodash gives a lot of useful functions to achieve finding the first duplicate index.
Using the _.findIndex() and _.isEqual() the following code will find the first duplicate index:

var duplicateIndex = _.findIndex(array, function(value, index, collection) {
  var equal = _.isEqual.bind(undefined, value);
  return _.findIndex(collection.slice(0, index), equal) !== -1;
});

或者更快但更详细:

var duplicateIndex = _.findIndex(array, function(value, index, collection) {
  var equal = _.isEqual.bind(undefined, value);
  return _.findIndex(collection, function(val, ind) {
     return ind < index && equal(val);
  }) !== -1;
});

请注意,如果不存在重复, -1 将被返回。

简而言之,算法遍历数组并回顾当前元素是否已经存在。如果是,只返回当前的迭代索引。

请检查工作演示

Notice that if no duplicate exists, -1 will be returned.
In a few words the algorithm iterates through array and looks back if the current element does not exist already. If it does, just return the current iteration index.
Please check the working demo.

这篇关于在Array中查找重复数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆