{"id":7502,"date":"2022-09-09T02:04:27","date_gmt":"2022-09-08T20:34:27","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-can-anyone-explain-the-following-javascript-code-for-unique-value-how-is-it-internally-compared-with-each-other-closed\/"},"modified":"2022-09-09T02:04:27","modified_gmt":"2022-09-08T20:34:27","slug":"solved-can-anyone-explain-the-following-javascript-code-for-unique-value-how-is-it-internally-compared-with-each-other-closed","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-can-anyone-explain-the-following-javascript-code-for-unique-value-how-is-it-internally-compared-with-each-other-closed\/","title":{"rendered":"[Solved] Can anyone explain the following JavaScript code for unique value how is it internally compared with each other [closed]"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-52297778\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"52297778\" data-parentid=\"52297644\" data-score=\"3\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p><code>return arr.indexOf(value) === arr.lastIndexOf(value);<\/code><\/p>\n<p>You have an array. Every element in the array has an index. .<code>indexOf()<\/code> will give you that index. So if an element is unique, the first index it appears at is the same as the last index. If the element would not be unique, the lastIndex will be higher than the (first)Index.<\/p>\n<p><code>})[0] || -1;   \/\/ how this works======================= B<\/code><\/p>\n<p>This will just return the first element of the filtered array. And if the array is empty or if the first element is falsey ( 0, null, undefined, &#8230; ), return -1. Its equivalent to <code>if ( array.length &amp;&amp; array[ 0 ] ) { return array[ 0 ]; } else return -1<\/code>.<\/p>\n<p>So the function in it&#8217;s entirity will just return you either the first unique element inside the array, or -1 if all elements have at least one duplicate.<\/p>\n<hr>\n<p>OK, Let&#8217;s try again:<\/p>\n<p>1) The function wants to return the first unique element that appears in an array. So if we have an array <code>[ 1, 1, 3, 7, 10, 10 ]<\/code>, the result should be 3, since 1 appears two times in the arrray.<\/p>\n<p>2) So the first step is making sure the array only contains unique elements. We do this by filtering out all the non-unique elements in the array.<br \/>\nTo do so, we use <code>Array.filter()<\/code>, the basic array method to filter elements out of an array. This <code>filter()<\/code> method expects us to provide a function that will return true or false. The method will then loop over the array, applying the function on each element. All the elements the function returns true for, will stay in the results array. All the elements we return false for, will get removed from the results.<\/p>\n<p>For example: <code>[ 1, 1, 3, 7, 10, 10 ].filter(( element ) =&gt; element &gt; 5);<\/code> In this example we return true if an element is bigger than 5, and false if its smaller than 5. So our result will be <code>[ 7, 10, 10 ]<\/code><\/p>\n<p>3) The function we want to actually use here, is to return true for elements that are unique and false for elements that aren&#8217;t. An unique element appears only once inside an array. So we can leverage <code>array.indexOf()<\/code> and <code>array.lastIndexOf()<\/code>. The former will return the first index an element appears at. The second the last index.<\/p>\n<p>Example: let&#8217;s assume our array is <code>[ 1, 1, 1, 9, 7, 10, 7, 21 ]<\/code> and that the element we&#8217;re checking is the value 1.<br \/>\nThe <code>.indexOf( 1 ) is 0<\/code>, since the first appearance of the value 1 inside the array, is the first index, since our array starts with the value 1.<br \/>\nThe <code>.lastIndexOf( 1 ) is 2<\/code>, since the 3rd element of the array is also 1.<\/p>\n<p>Compare this to the value 9.<br \/>\nThe <code>.indexOf( 9 ) is 3<\/code>, since it&#8217;s the 4th element of the array.<br \/>\nThe <code>.lastIndexOf( 9 ) is also 3<\/code>, since there&#8217;s only one 9 inside the array. So both indexOf and lastIndexOf find that same index.<\/p>\n<p>4) Once we have the index and lastIndex of an element , we then compare them:<br \/>\n<code>arr.indexOf(value) === arr.lastIndexOf(value)<\/code>.<br \/>\nSo let&#8217;s continue with the previous example: <code>[ 1, 1, 1, 9, 7, 10, 7, 21 ]<\/code>.<\/p>\n<p>For the value 1, the index was 0 and the lastIndex was 2. Compared to eachother, this is false, since <code>( 1 === 2 ) = false;<\/code>. So we return false for all the values 1 inside the array and hence, they get filtered away in the result.<\/p>\n<p>For the value 9, the index was 3 and the lastIndex as well. So our comparison becomes <code>( 3 === 3 ) = true;<\/code>. So we return true from inside the filter function and hence, 9 gets to stay inside the results.<\/p>\n<p>5) This logic gets used on all the elements in the source array. All the elements where the index isn&#8217;t the same as their lastIndex, get filtered away. All the elements where both indices are the same, are unique since they only appear once, and so they can stay.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">6<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Can anyone explain the following JavaScript code for unique value how is it internally compared with each other [closed] <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] return arr.indexOf(value) === arr.lastIndexOf(value); You have an array. Every element in the array has an index. .indexOf() will give you that index. So if an element is unique, the first index it appears at is the same as the last index. If the element would not be unique, the lastIndex will be higher than &#8230; <a title=\"[Solved] Can anyone explain the following JavaScript code for unique value how is it internally compared with each other [closed]\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-can-anyone-explain-the-following-javascript-code-for-unique-value-how-is-it-internally-compared-with-each-other-closed\/\" aria-label=\"More on [Solved] Can anyone explain the following JavaScript code for unique value how is it internally compared with each other [closed]\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[333],"class_list":["post-7502","post","type-post","status-publish","format-standard","hentry","category-solved","tag-javascript"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Can anyone explain the following JavaScript code for unique value how is it internally compared with each other [closed] - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-can-anyone-explain-the-following-javascript-code-for-unique-value-how-is-it-internally-compared-with-each-other-closed\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Can anyone explain the following JavaScript code for unique value how is it internally compared with each other [closed] - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] return arr.indexOf(value) === arr.lastIndexOf(value); You have an array. Every element in the array has an index. .indexOf() will give you that index. So if an element is unique, the first index it appears at is the same as the last index. If the element would not be unique, the lastIndex will be higher than ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-can-anyone-explain-the-following-javascript-code-for-unique-value-how-is-it-internally-compared-with-each-other-closed\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-08T20:34:27+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-can-anyone-explain-the-following-javascript-code-for-unique-value-how-is-it-internally-compared-with-each-other-closed\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-can-anyone-explain-the-following-javascript-code-for-unique-value-how-is-it-internally-compared-with-each-other-closed\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Can anyone explain the following JavaScript code for unique value how is it internally compared with each other [closed]\",\"datePublished\":\"2022-09-08T20:34:27+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-can-anyone-explain-the-following-javascript-code-for-unique-value-how-is-it-internally-compared-with-each-other-closed\/\"},\"wordCount\":588,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"javascript\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-can-anyone-explain-the-following-javascript-code-for-unique-value-how-is-it-internally-compared-with-each-other-closed\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-can-anyone-explain-the-following-javascript-code-for-unique-value-how-is-it-internally-compared-with-each-other-closed\/\",\"name\":\"[Solved] Can anyone explain the following JavaScript code for unique value how is it internally compared with each other [closed] - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-09-08T20:34:27+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-can-anyone-explain-the-following-javascript-code-for-unique-value-how-is-it-internally-compared-with-each-other-closed\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-can-anyone-explain-the-following-javascript-code-for-unique-value-how-is-it-internally-compared-with-each-other-closed\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-can-anyone-explain-the-following-javascript-code-for-unique-value-how-is-it-internally-compared-with-each-other-closed\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Can anyone explain the following JavaScript code for unique value how is it internally compared with each other [closed]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Can anyone explain the following JavaScript code for unique value how is it internally compared with each other [closed] - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-can-anyone-explain-the-following-javascript-code-for-unique-value-how-is-it-internally-compared-with-each-other-closed\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Can anyone explain the following JavaScript code for unique value how is it internally compared with each other [closed] - JassWeb","og_description":"[ad_1] return arr.indexOf(value) === arr.lastIndexOf(value); You have an array. Every element in the array has an index. .indexOf() will give you that index. So if an element is unique, the first index it appears at is the same as the last index. If the element would not be unique, the lastIndex will be higher than ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-can-anyone-explain-the-following-javascript-code-for-unique-value-how-is-it-internally-compared-with-each-other-closed\/","og_site_name":"JassWeb","article_published_time":"2022-09-08T20:34:27+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-can-anyone-explain-the-following-javascript-code-for-unique-value-how-is-it-internally-compared-with-each-other-closed\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-can-anyone-explain-the-following-javascript-code-for-unique-value-how-is-it-internally-compared-with-each-other-closed\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Can anyone explain the following JavaScript code for unique value how is it internally compared with each other [closed]","datePublished":"2022-09-08T20:34:27+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-can-anyone-explain-the-following-javascript-code-for-unique-value-how-is-it-internally-compared-with-each-other-closed\/"},"wordCount":588,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["javascript"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-can-anyone-explain-the-following-javascript-code-for-unique-value-how-is-it-internally-compared-with-each-other-closed\/","url":"https:\/\/jassweb.com\/solved\/solved-can-anyone-explain-the-following-javascript-code-for-unique-value-how-is-it-internally-compared-with-each-other-closed\/","name":"[Solved] Can anyone explain the following JavaScript code for unique value how is it internally compared with each other [closed] - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-09-08T20:34:27+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-can-anyone-explain-the-following-javascript-code-for-unique-value-how-is-it-internally-compared-with-each-other-closed\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-can-anyone-explain-the-following-javascript-code-for-unique-value-how-is-it-internally-compared-with-each-other-closed\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-can-anyone-explain-the-following-javascript-code-for-unique-value-how-is-it-internally-compared-with-each-other-closed\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Can anyone explain the following JavaScript code for unique value how is it internally compared with each other [closed]"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/7502","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=7502"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/7502\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=7502"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=7502"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=7502"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}