1

I was looking for a solution to finding multiple values in an array, and found this:

function find_duplicate_in_array(arra1) {
  var object = {};
  var result = [];

  arra1.forEach(function(item) {
    if (!object[item])
      object[item] = 0;
    object[item] += 1;
  })

  for (var prop in object) {
    if (object[prop] >= 2) {
      result.push(prop);
    }
  }
  return result;
}

console.log(find_duplicate_in_array([1, 2, -2, 4, 5, 4, 7, 8, 7, 7, 71, 3, 6]));

I don't understand what is happening. Specifically this:

object[item] = 0;
object[item] +=1;

So... for each element in the array, if the element is not in temporary object add element at index 0, and then +1?.

What's going on? Would someone please explain line by line. I am new to JavaScript.

halfer
  • 19,824
  • 17
  • 99
  • 186
Shaz
  • 1,443
  • 1
  • 27
  • 67
  • 1
    If `object` does not have the `[item]` property yet, it is assigned to `0` so it can then be added to with `+= 1`. (nothing to do with index 0) – CertainPerformance Nov 17 '18 at 08:28
  • If it doesn't have to do with index then is it to keep count? If so, why not just add +1 directly at first? – Shaz Nov 17 '18 at 08:34
  • Use lodash https://stackoverflow.com/a/31681942/5821253 – Enthusiastic Developer Nov 17 '18 at 08:37
  • Another possible cause for confusion is the name `object`, which would have been better off with almost any other name. I would have used `counters`. – Mr Lister Nov 17 '18 at 09:31
  • An if statement without `{}` is just going to crash and burn someone's understanding of how things work, especially when there's a single if in the next `for` loop that does have `{}` - code review fail! – StudioTime Nov 17 '18 at 18:48

2 Answers2

2

Here is the code, with each line commented! I hope it will help you ;)

function find_duplicate_in_array(arra1) {

  // Temporary count of each item in the input array/
  var object = {};
  // Final result containing each item that has been seen more than one time.
  var result = [];

  // For each item of the array...
  arra1.forEach(function (item) {
    // If it is not in the temporary object, initialize it to 0.
    if(!object[item])
      object[item] = 0;
    // Add one since we just have found it!  
    object[item] += 1;
  })


  // Now, every item of the input array has been counted in object.
  // For each item of object:
  for (var prop in object) {
    // If it has been counted more than one time, add it to the result.
    if(object[prop] >= 2) {
      result.push(prop);
    }
  }

  // Return the result.
  return result;

}

console.log(find_duplicate_in_array([1, 2, -2, 4, 5, 4, 7, 8, 7, 7, 71, 3, 6]));

The complexity here is on those lines:

if(!object[item])
  object[item] = 0;
object[item] += 1;

It is the same than the more stricter notation:

if(!object[item]) {
  object[item] = 0;
}
object[item] += 1;

If you don't set curly braces, only the next instruction will be executed!

sjahan
  • 5,720
  • 3
  • 19
  • 42
  • "_Add one since we just have found it!_" Nope, "Add one always" is what the line does. Missing curly braces are a bit confusing. – Teemu Nov 17 '18 at 08:37
  • Ah, so object[item] = 0; is to initialize it, and then the correct occurrence is calculated with object[item] +=1; rightt? – Shaz Nov 17 '18 at 08:50
  • 1
    @Shaz that's it. After an `if` statement, if there is no curly braces, only the next instruction is executed! It can be quite confusing and a dangerous shortcut if the developers are not very strict. Of course, if it hasn't been initialized to `0`, doing `+1` would get `NaN` as a result, so you have to set it to `0` on the first time. – sjahan Nov 17 '18 at 08:55
1

Maybe you are missing the block statement { ... } of the if statement, which would be

if (!object[item]) {
    object[item] = 0;
}
object[item] += 1;

which means, if object[item] is not truthy, then assign zero to object[item].

Then increment this value.

if (!object[item])    // no block statement
    object[item] = 0; // then part finished with semicolon
object[item] += 1;    // code after the condition

The given code is a valid change of the above by taking only a single statement, which is finished with semicolon. In this case you do not need a block statement.

Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • Yes. The comments to the question make it sound like the absence of `{` and `}` is a mistake, but it's important to point out that it's not. It is part of the language syntax. And when the OP will look at other people's code in the future, they will come across the same construct more often and they should be aware that it works like that. – Mr Lister Nov 18 '18 at 08:04