Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find Duplicate Array within 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
  ]
]

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)

With would give the "unique" version of the list:

[ 
    [ 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.

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. Returning just the duplicate or at least the comparison difference with the "unique list".

  2. 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".

like image 858
Neil Lunn Avatar asked Apr 22 '16 04:04

Neil Lunn


People also ask

How do you find duplicate numbers in an array if it contains multiple duplicates?

Simple Approach: The idea is to use nested loop and for each element check if the element is present in the array more than once or not. If present, then store it in a Hash-map. Otherwise, continue checking other elements.

How do you remove duplicate arrays from an array of arrays?

To remove duplicates from an array: First, convert an array of duplicates to a Set . The new Set will implicitly remove duplicate elements. Then, convert the set back to an array.

How do you find the number of duplicates in an array?

Given an array of n + 1 integers between 1 and n, find one of the duplicates. If there are multiple possible answers, return one of the duplicates. If there is no duplicate, return -1. Example: Input: [1, 2, 2, 3]

How to check if array contains duplicates within k distance?

Given an unsorted array that may contain duplicates. Also given a number k which is smaller than size of array. Write a function that returns true if array contains duplicates within k distance. Examples: Input: k = 3, arr[] = {1, 2, 3, 4, 1, 2, 3, 4} Output: false All duplicates are more than k distance away.

How to find duplicates between 1 and N in Python?

Given an array of n + 1 integers between 1 and n, find one of the duplicates. If there are multiple possible answers, return one of the duplicates. If there is no duplicate, return -1. Example: Input: [1, 2, 2, 3] Before to see the solutions, let’s talk a bit about the problem. We get an array of n + 1 element with integers between 1 and n.

How to remove duplicate values from an array in JavaScript?

The shorthand method for removing duplicate values from an array can also be used and written by utilizing a Set (), a JavaScript object that allows you to store unique values of any type: Finding and removing duplicate values from JavaScript arrays is pretty straightforward, but does require some knowledge of how arrays work.


1 Answers

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;
});

or a bit faster but more verbose:

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;
});

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.

like image 55
Dmitri Pavlutin Avatar answered Sep 21 '22 02:09

Dmitri Pavlutin