Why is reduce performing better in this use case of finding duplicates in an array [closed]
Clash Royale CLAN TAG #URR8PPP Why is reduce performing better in this use case of finding duplicates in an array [closed] Recent problem stated that given an array of very large size we need to find first duplicate value and return it (not the index but the value). So given an example array of [1,2,3,4,5,3] we need to return 3 since it is the first value with a duplicate in the array. [1,2,3,4,5,3] 3 Now the array in question could be 100K and much larger. Given those requirements there are few solutions that come to mind right away. 1.Using ES6 with Set solution: function firstDuplicate(a) var r = new Set(); for (var e of a) if (r.has(e)) return e; else r.add(e); 2.Using for loop with lastIndexOf: const firstDuplicate = (arr) => for(var i = 0; i < arr.length ; i++) if(arr.lastIndexOf(arr[i]) > i) return arr[i] 3.Using reduce & lastIndexOf: Note: I am aware that this approach mutates the input via splice used to exit from the reduce... splice const...