javascript - Find Duplicate Array within Array -


given array of arrays, efficient way of identifying duplicate item?

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

i've been working on lodash accepted dependency, , how return "unique" list using _.uniqwith , _.isequal:

_.uniqwith(array,_.isequal) 

with give "unique" version of list:

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

but rather reporting unique elements, need element duplicated, , ideally index of first occurrence.

is covered in lodash library combination of methods i'm missing? or going have live writing loops compare elements.

probably overtired on this, fresh eyes on problem welcome.

trying not rewrite functions if there library methods suit, stuck with:

  1. returning duplicate or @ least comparison difference "unique list".

  2. basically identifying "index of" array within array. though suppose can filter reduction _.isequal once duplicate item identified.

trying avoid creating object hash/map , counting occurrences of keys here well, or @ least not separate object, , can done functionally "in-line".

lodash gives lot of useful functions achieve finding first duplicate index.
using _.findindex() , _.isequal() following code find 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 bit faster 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 if no duplicate exists, -1 returned.
in few words algorithm iterates through array , looks if current element not exist already. if does, return current iteration index.
please check working demo.


Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -