{"id":929,"date":"2023-12-13T07:15:10","date_gmt":"2023-12-13T07:15:10","guid":{"rendered":"https:\/\/favtutor.com\/articles\/?p=929"},"modified":"2023-12-13T07:15:12","modified_gmt":"2023-12-13T07:15:12","slug":"remove-duplicates-array-javascript","status":"publish","type":"post","link":"https:\/\/favtutor.com\/articles\/remove-duplicates-array-javascript\/","title":{"rendered":"Remove Duplicates from Array in JavaScript (with code)"},"content":{"rendered":"\n<p>We frequently come across situations where we need to remove duplicate elements from an array while problem-solving. It&#8217;s important to have effective techniques to handle the duplicates as they can create ambiguities in certain situations. This article discusses several methods to remove duplicates from JavaScript arrays.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>How to Remove Duplicate Elements from JS Arrays?<\/strong><\/h2>\n\n\n\n<p><strong>Removing duplicates means removing the values in the array that have occurred more than once. <\/strong>Let\u2019s say, arr = [1,2,3,4,4,5]. In this, the number four is present twice. We have to reduce this array to [1,2,3,4,5] after removing the duplicates of 4.<\/p>\n\n\n\n<p>This can be done using several methods. Let\u2019s discuss each of them one by one.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1) Using the indexOf() Method<\/strong><\/h3>\n\n\n\n<p><strong>The simplest method to remove duplicate elements from an array is using indexOf() method.<\/strong> The indexOf() method is used to find the first index of occurrence of an array element. If the element does not exist in an array, the function simply returns a \u201c-1\u201d instead of returning any index.<\/p>\n\n\n\n<p>We iterate over the elements in the array. We check if the element exists in the resultant array. If it does, we do not push it into the array. If it doesn\u2019t, we push into the resultant array.<\/p>\n\n\n\n<p>Let us see the code of creating an array with no duplicate elements from the array with duplicate elements:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring an array\nlet ogArray=[1,2,3,4,4,5]\n\n\/\/create a new array to store unique elements only\nlet newArray=[];\n\n\/\/add unique elements to the newArray\nfor(let i=0;i&lt;ogArray.length;i++) {\n    \/\/checking if the element of ogArray exists in the newArray already\n    if (newArray.indexOf(ogArray[i]) === -1) {\n        \/\/if not, push in the newArray\n        newArray.push(ogArray[i]);\n    }\n}\n\n\/\/printing the newArray\nconsole.log(newArray);<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>&#91; 1, 2, 3, 4, 5 ]<\/code><\/pre>\n\n\n\n<p>In this method, we first created an empty array called newArray. We iterated over the original array using the for loop. For every element of the original array, we check whether the element is present in the newArray already or not using the indexOf() function. <\/p>\n\n\n\n<p>If the indexOf() function returns a negative one, it means that the element is not present in the newArray. Hence, we add the element into the newArray.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2) Using the filter() Method<\/strong><\/h3>\n\n\n\n<p>Another method to remove the duplicates from an array is the <a href=\"https:\/\/favtutor.com\/articles\/javascript-array-filter\/\" data-type=\"link\" data-id=\"https:\/\/favtutor.com\/articles\/javascript-array-filter\/\">filter() method<\/a>. The filter() creates a new array according to the condition passed in the filter method.&nbsp;<\/p>\n\n\n\n<p>The syntax of the method is given as follows.<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>let newArray = ogArray.filter(function(element) {  \n    \/\/ return true to keep the element, false to discard it \n});<\/code><\/pre>\n\n\n\n<p>where,<\/p>\n\n\n\n<p><strong>newArray<\/strong>: the new array formed after applying the filter function&nbsp;<\/p>\n\n\n\n<p><strong>ogArray<\/strong>: original array<\/p>\n\n\n\n<p><strong>function: <\/strong>the function that will be called within the filter function to convert into a new array.<\/p>\n\n\n\n<p>Now, let us take a look at how to remove duplicates using the filter() method. We will compare the index of each element with its first occurrence in the array. If the index matches the first occurrence, we include it in the filtered array and it will be removed from the array.<\/p>\n\n\n\n<p>Here\u2019s how we will write the code:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring an array\nlet ogArray=[1,2,3,4,4,5]\n\n\/\/using the filter function\nlet newArray = ogArray.filter((item, index) =&gt; ogArray.indexOf(item) === index);\n\n\/\/printing the new filtered array\nconsole.log(newArray);<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>&#91; 1, 2, 3, 4, 5 ]<\/code><\/pre>\n\n\n\n<p>We iterate over the array elements and check if it has occurred before. If yes, we do not include it in the new array, else, we include it. We have obtained the desired result.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3) Using the Set() Method<\/strong><\/h3>\n\n\n\n<p>The third method is using the Set() method. A set is a data type that stores unique values. No duplicate values are found in a set. A set can hold either unique integers, unique characters, or unique strings.&nbsp;<\/p>\n\n\n\n<p>The Set() method is used to create a set of unique values. Further, the set can be converted to an array using the spread operator.<\/p>\n\n\n\n<p>The syntax of the Set() method is as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>let newSet=new Set(ogArray);<\/code><\/pre>\n\n\n\n<p>The set can be converted to an array using the spread operator as shown below:<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>let newArray=&#91;...newSet];<\/code><\/pre>\n\n\n\n<p>where,&nbsp;<\/p>\n\n\n\n<p><strong>newSet<\/strong>: set returned by the Set() method<\/p>\n\n\n\n<p><strong>ogArray<\/strong>: the original array on which filter() is applied<\/p>\n\n\n\n<p><strong>newArray<\/strong>: the new array formed from set using spread operator<\/p>\n\n\n\n<p>Now, let us try to remove duplicates from an array. Here\u2019s an example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring an array\nlet ogArray=[1,2,3,4,4,5]\n\n\/\/using the Set() Method\nlet newSet = new Set(ogArray)\n\n\/\/converting set to array\nlet newArray=[...newSet];\n\n\/\/printing the new filtered array\nconsole.log(newArray);<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>&#91; 1, 2, 3, 4, 5 ]<\/code><\/pre>\n\n\n\n<p>We convert the array to a set to remove the duplicates. The set with unique values of the original array is converted back to an array. We have obtained the desired result.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>This article discussed various methods to remove duplicates from the array in JavaScript. We used the indexOf() function, the filter() method, and the Set() method to remove duplicates from the array. Each method has its advantages and disadvantages, and the choice depends on factors such as code readability.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We have discussed various methods to remove duplicate elements from an array in JavaScript, along with examples.<\/p>\n","protected":false},"author":9,"featured_media":932,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jnews-multi-image_gallery":[],"jnews_single_post":null,"jnews_primary_category":{"id":"","hide":""},"footnotes":""},"categories":[9],"tags":[11,13],"class_list":["post-929","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-development","tag-javascript","tag-javascript-array"],"_links":{"self":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/929","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/comments?post=929"}],"version-history":[{"count":5,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/929\/revisions"}],"predecessor-version":[{"id":960,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/929\/revisions\/960"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media\/932"}],"wp:attachment":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media?parent=929"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/categories?post=929"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/tags?post=929"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}