# Finding duplicates in array?

can someone explain how this code works in steps?it is hard to grasp?

``````               //determining occurence of dulplicate
var method2 = function(a) {
var counts = [];
for(var i = 0; i <= a.length; i++) {
if(counts[a[i]] === undefined) {
counts[a[i]] = 1;
} else {
return true;
}
}

return false;
}
``````

Which part(s) do you understand and which part(s) do you not understand?

whats happening in fourth and fifth lines?

This?

``````if(counts[a[i]] === undefined) {
counts[a[i]] = 1;
``````

We know that â€ścountsâ€ť is an array because it was defined immediately before the for loop.
What is not so clear is what is being used as the counts array key. It is â€śa[i]â€ť, the â€śaâ€ť being whatever was passed into the function, the â€śiâ€ť being an incremented value.

So whatever â€śa[i]â€ť happens to be during that iteration of the loop is used as a key for the counts array. If the counts array with that key is undefined, it is assigned a value (in this case, 1),

If a subsequent value of â€śa[i]â€ť is the same as a previously encountered â€śa[i]â€ť it will not be undefined and that part of the if wonâ€™t test as truthy.

2 Likes

thanks got itâ€¦

Using for loop getting desired result but forEach returning false for both duplicate and non duplicate values,Is any bug in the code.

`````` var arrs = ['fame','ram','sam','fame'];

//using forEach

function find_dup(arr){

var counts = [];

arr.forEach(function(element){
if(counts[element] === undefined){
counts[element] = 1;

}else{
return true;
}
});

return false;
}

//using forloop

function find_dup(arr){
var counts = [];
for(var i = 0; i < arr.length; i++) {
if(counts[arr[i]] === undefined) {
counts[arr[i]] = 1;

} else {
return true;
}
}

return false;
}

console.log(find_dup(arrs));
``````

Yes. Returning something in a callback has no effect on the outer function.

Array.forEach() is simply the wrong function. Array.some() or Array.reduce() are certainly more appropriate.

1 Like

Ok you mean that we cant return anything from the callback but we can call another function from callback.

Thatâ€™s oversimplification (and it depends what you intend by return). Return values do have a purpose, e.g. in Array.map() the return value determines what the new array values become. Nevertheless return values in callbacks have no effect on the outer function (i.e. you canâ€™t use them to abort the outer function).

It should also be pointed out thereâ€™s a mismatch between the title of this thread and the function given.

The function given doesnâ€™t find the duplicates in the array. It finds IF there IS ANY duplicate in the array. The former would be expected to return an array containing the duplicates. The latter returns a boolean value.

1 Like

I should point out that return values do sometimes have an effect on the outer function, where Array.prototype.filter uses the return value to decide which items get included or not, and Array.prototype.some does an early return when `true` is returned from the inner function.

Incidentally, if I was going to write that function for a Boolean value, it would be:

`if(arrs.length == new Set(arrs).size)`
(Because by definition, a Set constructed from the array is a set of unique values in the array)

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.