All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

Remove Duplicates From Array JavaScript

Last Updated : Mar 11, 2024

Remove Duplicates From Array JavaScript

In this tutorial we will show you the solution of remove duplicates from array JavaScript, here we using for loop for iterate array elements then using if condition we can easily find duplicates by comparing each other values.

After that we can remove by using available methods of pop(), shift() or slice() in javascript. Here we used pop() method for remove that duplicates from array.

Step By Step Guide On Remove Duplicates From Array JavaScript :-

Here we defined two div elements for appends before and after removing duplicates from an array ‘arr’ result on webpage.

Using nested for loop we comparing for each one value with next index of values till last value in array then only we can found all duplicates from array ‘arr’.

When founding second duplicated number we are removing that value from array by using ‘pop()’ method.

<!DOCTYPE html>
        <title>REMOVE DUPLICATES IN ARRAY</title>
        <div id="res1"></div>
        <div id="res2"></div>
            const arr=['12','34','45','89','45','12'];
            document.getElementById('res1').innerHTML="Before removing duplicates from array :"+arr;
                    if(arr[i] == arr[j]){
            document.getElementById('res2').innerHTML="<br>After removed duplicates from array :"+arr;
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contain information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  5. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  6. Here we defined two div elements ‘res1,res2’ for appending before and after removing duplicates from array ‘arr’ results on webpage.
  7. In script we declared array variable ‘arr’ for initialize values ‘['12','34','45','89','45','12']’. Then we appending array on webpage for knowing difference before and after removed duplications.
  8. In first for loop we iterating array elements from index ‘0’ then in second for loop we iterating remaining array elements from next index of first loop value.
  9. Using if condition we checking each value with remaining index of values and then if we found any duplicate at the time we removing that duplicate value by pop() method.
  10. The pop() method will easily removes one value from array ‘arr’ without affecting array values order.
  11. The break statement will stops the second for loop iterations after found duplicate so it will start from first for loop for iterate next value.
  12. Finally we appended array ‘arr’ on webpage for user clarifications after removed duplicates from array ‘arr’.
  13. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion now we are able to know how to remove duplicates from array using javascript.

When we executes program on browser we can see two appended array results before and after removed duplicates from array.

We can also change or modify the above defined array ‘arr’ as per yours own wish result will same.

If you’re trying to use this concept on array with string values then that will possible.

I hope this tutorial on remove duplicates from array JavaScript helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪