4 ms·
Open up web inspector. var arr = (paste) arr.sort() for(var i = 0;i<arr.length;i++){if(arr[i+1] == arr[i]){console.log(arr[i])}}
by annon 16y ago
Open up web inspector.
var arr = (paste)
arr.sort()
for(var i = 0;i<arr.length;i++){if(arr[i+1] == arr[i]){console.log(arr[i])}}
- mitjak 16y agoOr the recursive version, upon sorting: function find_dupe(a){ if(a.length==0) return -1; if (a[0]==a[1]) return a[0]; else return find_dupe(a.slice(1,a.length)); } BTW, are loops more efficient than recursion in JS? It sure seems like a lot of JS libraries out there including jQuery avoid recursion like plague.
- annon 16y agoFor recursion vs. looping, it really depends on what you're doing. In my js experience though, most of the time looping is either faster or equal to recursion, while being significantly easier to understand at a quick glance. In this case, the loop will outperform recursion because of all of the slice calls in the recursive version.