Given a nested array, what is the best approach to finding duplicates efficiently?
var array = [
[
11.31866455078125,
44.53836644772605
],
[ // <-- Here's the duplicate
11.31866455078125,
44.53836644772605
],
[
11.371536254882812,
44.53836644772605
],
[
11.371536254882812,
44.50140292110874
]
]
While using lodash
, I have managed to get the unique elements by utilizing _.uniqWith
and _.isEqual
:
_.uniqWith(array, _.isEqual)
This code returns:
[
[ 11.31866455078125, 44.53836644772605 ],
[ 11.371536254882812, 44.53836644772605 ],
[ 11.371536254882812, 44.50140292110874 ]
]
However, instead of just obtaining the unique items, my goal is to locate the duplicated element and ideally determine the index of its first occurrence.
I am wondering if there is a method within the lodash
library that can help achieve this task, or if manual comparison through loops is necessary. Any insights would be appreciated.
My current constraints prevent me from rewriting functions, so I am exploring options such as:
Finding only the duplicate element or noting the difference between it and the "unique list".
Identifying the index of an array within another array, possibly with filtering and reducing once the duplicate item is found using
_.isEqual
.
In addition, I am aiming to avoid creating separate objects like Hash/Map to count occurrences of keys and prefer a functional approach that does not necessitate additional data structures.