{"id":9425,"date":"2013-02-01T13:48:09","date_gmt":"2013-02-01T08:18:09","guid":{"rendered":"https:\/\/tothenewco.pro\/blog\/?p=9425"},"modified":"2016-12-16T11:01:14","modified_gmt":"2016-12-16T05:31:14","slug":"removing-duplicate-elements-from-an-array-using-reduce-method","status":"publish","type":"post","link":"https:\/\/tothenewco.pro\/blog\/removing-duplicate-elements-from-an-array-using-reduce-method\/","title":{"rendered":"How to Remove Duplicate Elements from Array in JavaScript"},"content":{"rendered":"<p>Arrays in JavaScript are daunting and particularly difficult in nature. The fundamental reason for making this statement is that <a title=\"react.js development\" href=\"https:\/\/tothenewco.pro\/front-end-angularjs-development\">JavaScript<\/a> does not provide as many functions\/methods for array operations as several other languages do. As a result, an array operation as simple as removing duplicate elements from an array may also be particularly intimidating.<\/p>\n<p>A possible approach for accomplishing this task of removing duplicate elements from an array in JavaScript is using the method reduce(). The method reduce() performs execution of the callback function for every element present in the array. It automatically excludes all the holes in the array and expects 4 arguments. These arguments include \u2013<\/p>\n<ul>\n<li>Initial value<\/li>\n<li>Value of the current element<\/li>\n<li>Current index<\/li>\n<li>Array to be used for iterations<\/li>\n<\/ul>\n<p>On its first execution, if the initial value is provided, the value of the previous value is equal to the initial value and the current value is equal to the value of the first element of the array. On the other hand, if no initial value is given, the value of previous is equal to the value of the first element of the array and the value of current is equal to the value of the second element of the array.<\/p>\n<p>In order to understand the concept, let us consider an example.<\/p>\n<p>[js]<br \/>\nArray.prototype.unique= function ()<br \/>\n{<br \/>\n  return this.reduce(function(previous, current, index, array)<br \/>\n   {<br \/>\n     previous[current.toString()+typeof(current)]=current;<br \/>\n     return array.length-1 == index ? Object.keys(previous).reduce(function(prev,cur)<br \/>\n       {<br \/>\n          prev.push(previous[cur]);<br \/>\n          return prev;<br \/>\n       },[]) : previous;<br \/>\n   }, {});<br \/>\n};<\/p>\n<p>console.log([2,5,&quot;4&quot;, 9,4,2,7,2].unique());<br \/>\n[\/js]<\/p>\n<h3>Output:<\/h3>\n<p>[js]<br \/>\n[ 2, 5, &#8216;4&#8217;, 9, 4, 7 ]<br \/>\n[\/js]<\/p>\n<p>The methodology followed in the example illustrated above, adds a method named unique() in the prototype of <strong>Array <\/strong>class. This method unique() makes use of the method <strong>reduce()<\/strong>. The argument given for initial value is an empty map. In the call-back method, we are pushing unique key value pairs of the map thereby eliminating the duplicate elements. The method Object.keys() provides these unique keys. The nested method reduce() returns the key values in the form of an array.<\/p>\n<p>Besides this, code is also equipped to handle the case where a user enters a string and an integer of the same value. An example of this scenario is \u201c4\u2033 and 4. In order to handle such a situation, the example makes use of the <strong>typeof()<\/strong> method, which is concatenated with current.toString(). This ensures that different type of values, numerals and alphabets are identified separately and handled as found appropriate.<\/p>\n<p>Try it on online node console www.node-console.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Arrays in JavaScript are daunting and particularly difficult in nature. The fundamental reason for making this statement is that JavaScript does not provide as many functions\/methods for array operations as several other languages do. As a result, an array operation as simple as removing duplicate elements from an array may also be particularly intimidating. A [&hellip;]<\/p>\n","protected":false},"author":65,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":4,"footnotes":""},"categories":[1185],"tags":[4299,1124,1127,357],"class_list":["post-9425","post","type-post","status-publish","format-standard","hentry","category-node-js-2","tag-array-in-javascript","tag-node-js","tag-reduce","tag-unique"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.0.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"Steps to Remove Duplicate Elements from Array in JavaScript\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Sakshi Tyagi\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/tothenewco.pro\/blog\/removing-duplicate-elements-from-an-array-using-reduce-method\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.0.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"TO THE NEW BLOG\" \/>\n\t\t<meta property=\"og:type\" content=\"blog\" \/>\n\t\t<meta property=\"og:title\" content=\"How to Remove Duplicate Elements from Array in JavaScript | TO THE NEW Blog\" \/>\n\t\t<meta property=\"og:description\" content=\"Steps to Remove Duplicate Elements from Array in JavaScript\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/tothenewco.pro\/blog\/removing-duplicate-elements-from-an-array-using-reduce-method\/\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/tothenewco.pro\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/tothenewco.pro\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:site\" content=\"@tothenew\" \/>\n\t\t<meta name=\"twitter:title\" content=\"How to Remove Duplicate Elements from Array in JavaScript | TO THE NEW Blog\" \/>\n\t\t<meta name=\"twitter:description\" content=\"Steps to Remove Duplicate Elements from Array in JavaScript\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/tothenewco.pro\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/removing-duplicate-elements-from-an-array-using-reduce-method\\\/#article\",\"name\":\"How to Remove Duplicate Elements from Array in JavaScript | TO THE NEW Blog\",\"headline\":\"How to Remove Duplicate Elements from Array in JavaScript\",\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/sakshi\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"},\"datePublished\":\"2013-02-01T13:48:09+05:30\",\"dateModified\":\"2016-12-16T11:01:14+05:30\",\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/removing-duplicate-elements-from-an-array-using-reduce-method\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/removing-duplicate-elements-from-an-array-using-reduce-method\\\/#webpage\"},\"articleSection\":\"Node.js, array in javascript, node.js, reduce(), unique\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/removing-duplicate-elements-from-an-array-using-reduce-method\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/node-js-2\\\/#listItem\",\"name\":\"Node.js\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/node-js-2\\\/#listItem\",\"position\":2,\"name\":\"Node.js\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/node-js-2\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/removing-duplicate-elements-from-an-array-using-reduce-method\\\/#listItem\",\"name\":\"How to Remove Duplicate Elements from Array in JavaScript\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/removing-duplicate-elements-from-an-array-using-reduce-method\\\/#listItem\",\"position\":3,\"name\":\"How to Remove Duplicate Elements from Array in JavaScript\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/node-js-2\\\/#listItem\",\"name\":\"Node.js\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\",\"name\":\"TO THE NEW Blog\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/sakshi\\\/#author\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/sakshi\\\/\",\"name\":\"Sakshi Tyagi\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/removing-duplicate-elements-from-an-array-using-reduce-method\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a42c6deb088e18815dfcfed629c77b032d17bf973881421880b9c5e4ba713739?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Sakshi Tyagi\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/removing-duplicate-elements-from-an-array-using-reduce-method\\\/#webpage\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/removing-duplicate-elements-from-an-array-using-reduce-method\\\/\",\"name\":\"How to Remove Duplicate Elements from Array in JavaScript | TO THE NEW Blog\",\"description\":\"Steps to Remove Duplicate Elements from Array in JavaScript\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/removing-duplicate-elements-from-an-array-using-reduce-method\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/sakshi\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/sakshi\\\/#author\"},\"datePublished\":\"2013-02-01T13:48:09+05:30\",\"dateModified\":\"2016-12-16T11:01:14+05:30\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\",\"name\":\"TO THE NEW Blog\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"How to Remove Duplicate Elements from Array in JavaScript | TO THE NEW Blog","description":"Steps to Remove Duplicate Elements from Array in JavaScript","canonical_url":"https:\/\/tothenewco.pro\/blog\/removing-duplicate-elements-from-an-array-using-reduce-method\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/tothenewco.pro\/blog\/removing-duplicate-elements-from-an-array-using-reduce-method\/#article","name":"How to Remove Duplicate Elements from Array in JavaScript | TO THE NEW Blog","headline":"How to Remove Duplicate Elements from Array in JavaScript","author":{"@id":"https:\/\/tothenewco.pro\/blog\/author\/sakshi\/#author"},"publisher":{"@id":"https:\/\/tothenewco.pro\/blog\/#organization"},"datePublished":"2013-02-01T13:48:09+05:30","dateModified":"2016-12-16T11:01:14+05:30","inLanguage":"en-US","mainEntityOfPage":{"@id":"https:\/\/tothenewco.pro\/blog\/removing-duplicate-elements-from-an-array-using-reduce-method\/#webpage"},"isPartOf":{"@id":"https:\/\/tothenewco.pro\/blog\/removing-duplicate-elements-from-an-array-using-reduce-method\/#webpage"},"articleSection":"Node.js, array in javascript, node.js, reduce(), unique"},{"@type":"BreadcrumbList","@id":"https:\/\/tothenewco.pro\/blog\/removing-duplicate-elements-from-an-array-using-reduce-method\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/tothenewco.pro\/blog#listItem","position":1,"name":"Home","item":"https:\/\/tothenewco.pro\/blog","nextItem":{"@type":"ListItem","@id":"https:\/\/tothenewco.pro\/blog\/category\/node-js-2\/#listItem","name":"Node.js"}},{"@type":"ListItem","@id":"https:\/\/tothenewco.pro\/blog\/category\/node-js-2\/#listItem","position":2,"name":"Node.js","item":"https:\/\/tothenewco.pro\/blog\/category\/node-js-2\/","nextItem":{"@type":"ListItem","@id":"https:\/\/tothenewco.pro\/blog\/removing-duplicate-elements-from-an-array-using-reduce-method\/#listItem","name":"How to Remove Duplicate Elements from Array in JavaScript"},"previousItem":{"@type":"ListItem","@id":"https:\/\/tothenewco.pro\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/tothenewco.pro\/blog\/removing-duplicate-elements-from-an-array-using-reduce-method\/#listItem","position":3,"name":"How to Remove Duplicate Elements from Array in JavaScript","previousItem":{"@type":"ListItem","@id":"https:\/\/tothenewco.pro\/blog\/category\/node-js-2\/#listItem","name":"Node.js"}}]},{"@type":"Organization","@id":"https:\/\/tothenewco.pro\/blog\/#organization","name":"TO THE NEW Blog","url":"https:\/\/tothenewco.pro\/blog\/"},{"@type":"Person","@id":"https:\/\/tothenewco.pro\/blog\/author\/sakshi\/#author","url":"https:\/\/tothenewco.pro\/blog\/author\/sakshi\/","name":"Sakshi Tyagi","image":{"@type":"ImageObject","@id":"https:\/\/tothenewco.pro\/blog\/removing-duplicate-elements-from-an-array-using-reduce-method\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/a42c6deb088e18815dfcfed629c77b032d17bf973881421880b9c5e4ba713739?s=96&d=mm&r=g","width":96,"height":96,"caption":"Sakshi Tyagi"}},{"@type":"WebPage","@id":"https:\/\/tothenewco.pro\/blog\/removing-duplicate-elements-from-an-array-using-reduce-method\/#webpage","url":"https:\/\/tothenewco.pro\/blog\/removing-duplicate-elements-from-an-array-using-reduce-method\/","name":"How to Remove Duplicate Elements from Array in JavaScript | TO THE NEW Blog","description":"Steps to Remove Duplicate Elements from Array in JavaScript","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/tothenewco.pro\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/tothenewco.pro\/blog\/removing-duplicate-elements-from-an-array-using-reduce-method\/#breadcrumblist"},"author":{"@id":"https:\/\/tothenewco.pro\/blog\/author\/sakshi\/#author"},"creator":{"@id":"https:\/\/tothenewco.pro\/blog\/author\/sakshi\/#author"},"datePublished":"2013-02-01T13:48:09+05:30","dateModified":"2016-12-16T11:01:14+05:30"},{"@type":"WebSite","@id":"https:\/\/tothenewco.pro\/blog\/#website","url":"https:\/\/tothenewco.pro\/blog\/","name":"TO THE NEW Blog","inLanguage":"en-US","publisher":{"@id":"https:\/\/tothenewco.pro\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"TO THE NEW BLOG","og:type":"blog","og:title":"How to Remove Duplicate Elements from Array in JavaScript | TO THE NEW Blog","og:description":"Steps to Remove Duplicate Elements from Array in JavaScript","og:url":"https:\/\/tothenewco.pro\/blog\/removing-duplicate-elements-from-an-array-using-reduce-method\/","og:image":"https:\/\/tothenewco.pro\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","og:image:secure_url":"https:\/\/tothenewco.pro\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","twitter:card":"summary","twitter:site":"@tothenew","twitter:title":"How to Remove Duplicate Elements from Array in JavaScript | TO THE NEW Blog","twitter:description":"Steps to Remove Duplicate Elements from Array in JavaScript","twitter:image":"https:\/\/tothenewco.pro\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png"},"aioseo_meta_data":{"post_id":"9425","title":"How to Remove Duplicate Elements from Array in JavaScript | #site_title","description":"Steps to Remove Duplicate Elements from Array in JavaScript","keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":"","og_description":"","og_object_type":"blog","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":"","og_custom_url":null,"og_article_section":"","og_article_tags":"","twitter_use_og":false,"twitter_card":"summary","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"limit_modified_date":false,"created":"2021-04-29 17:10:08","updated":"2024-02-29 09:28:21","focus_keyword":null,"additional_keywords":null,"truseo_locale":null,"ai":null,"breadcrumb_settings":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/tothenewco.pro\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/tothenewco.pro\/blog\/category\/node-js-2\/\" title=\"Node.js\">Node.js<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tHow to Remove Duplicate Elements from Array in JavaScript\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/tothenewco.pro\/blog"},{"label":"Node.js","link":"https:\/\/tothenewco.pro\/blog\/category\/node-js-2\/"},{"label":"How to Remove Duplicate Elements from Array in JavaScript","link":"https:\/\/tothenewco.pro\/blog\/removing-duplicate-elements-from-an-array-using-reduce-method\/"}],"_links":{"self":[{"href":"https:\/\/tothenewco.pro\/blog\/wp-json\/wp\/v2\/posts\/9425","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tothenewco.pro\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tothenewco.pro\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tothenewco.pro\/blog\/wp-json\/wp\/v2\/users\/65"}],"replies":[{"embeddable":true,"href":"https:\/\/tothenewco.pro\/blog\/wp-json\/wp\/v2\/comments?post=9425"}],"version-history":[{"count":0,"href":"https:\/\/tothenewco.pro\/blog\/wp-json\/wp\/v2\/posts\/9425\/revisions"}],"wp:attachment":[{"href":"https:\/\/tothenewco.pro\/blog\/wp-json\/wp\/v2\/media?parent=9425"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tothenewco.pro\/blog\/wp-json\/wp\/v2\/categories?post=9425"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tothenewco.pro\/blog\/wp-json\/wp\/v2\/tags?post=9425"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}