JS Startup

Challenges, Tips & Tricks & Tutorials

JavaScript Quiz – array element duplicate count with for loop

JavaScript Quiz – array element duplicate count with for loop

Array Element Duplicate Count

JavaScript array contains an element of any data type and the element can be duplicate or unique. And there are many ways to calculate unique and duplicate array element count.

JavaScript Question

In this javascript quiz, find out array element duplicate count with for loop and object which store array element as property and increment.

First, we declare a variable arr using the let keyword and assign an javascript array.

let arr = [2, 3, 2, 1, 5, 3, 6];

In the next step, create another variable obj and assign blank object of it.

let obj = {}

Now, create a for loop and inside it obj assign array element value as key. In which we also increment a previous exist value but if not exist then assign 1.

for(let i=0; i< arr.length; i++){
  obj[arr[i]] = obj[arr[i]] ? obj[arr[i]]+1 : 1;

Finally, we are checking the value of obj for property 2 & 3. So, any idea what should be the output of the above code statement.

JavaScript Quiz Test

Here is the full javascript code snippet for you to solve.

let arr = [2, 3, 2, 1, 5, 3, 6];
let obj = {};

for(let i=0; i< arr.length; i++){
  obj[arr[i]] = obj[arr[i]] ? obj[arr[i]]+1 : 1;

console.log(obj[2]); // output => ? ?
console.log(obj[3]); // output => ? ?

// Made with ❤️, by @jsstartup

Answer – 2, 2

Click To Run Code


Now you the answer is 2, 2. But what actually happens in that javascript for loop. Let me explain.

First, make thing clear that we are counting element count which is inside an array. How,

On every iteration of for loop. we are checking that the array element is store in the obj variable. If it does not exist then we assign a value 1 to an object with the element as property.

We used a ternary operator here which is a shorthand technique to write if-else.

But if you console the whole obj object then you will get below object.


// output = >
  1: 1,
  2: 2,
  3: 2,
  5: 1,
  6: 1

At last, That’s why we get an answer 2, 2

Let me know in the comment section, what you think about it.

I hope you understand the concept and logic behind it.

But wait, i have something more for you –

Check out our other javascript quiz – 

  1. JS Quiz – access JSON string value is possible
  2. JavaScript Quiz – can divide assignment operator work in one go
  3. JavaScript Quiz – do act then while checking falsy
  4. JS Quiz – for loop iterate array in two ways


To participate in our javascript quiz or challenges, tutorial, tips & tricks make sure to join our jsstartup newsletter. So, you can able to participate in our daily challenges & learn the javascript concept.

And last but not the least, don’t forget to like, comment and share. It gives us a morale boost to remain to continue. Also, join our Facebook Page

If you have any questions, please feel free to ask me in the comment section and also let me know if you have any suggestions. As suggestions are always welcome.

Leave a Reply

Your email address will not be published.

Back to top
Share via
Copy link
Powered by Social Snap