<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Obieze Ezeugo Felistus</title>
    <description>The latest articles on DEV Community by Obieze Ezeugo Felistus (@felistus).</description>
    <link>https://dev.to/felistus</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F884628%2Fe4974f49-d6ff-4857-a52f-83b4dbeed586.jpeg</url>
      <title>DEV Community: Obieze Ezeugo Felistus</title>
      <link>https://dev.to/felistus</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/felistus"/>
    <language>en</language>
    <item>
      <title>Select unique elements from an Array using: lastIndexOf() &amp; indexOf()</title>
      <dc:creator>Obieze Ezeugo Felistus</dc:creator>
      <pubDate>Sat, 27 Aug 2022 23:25:00 +0000</pubDate>
      <link>https://dev.to/felistus/select-unique-elements-from-a-array-using-lastindexof-indexof-5dnh</link>
      <guid>https://dev.to/felistus/select-unique-elements-from-a-array-using-lastindexof-indexof-5dnh</guid>
      <description>&lt;p&gt;Hello guys! So, today I am going to show you how to use the following built-in array methods (&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/lastIndexOf"&gt;lastIndexOf()&lt;/a&gt; &amp;amp; &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf"&gt;indexOf()&lt;/a&gt;) to select unique elements from an array.&lt;/p&gt;

&lt;h2&gt;
  
  
  indexOf()
&lt;/h2&gt;

&lt;p&gt;The indexOf() method of the array returns the first index or position at which a given element can be found in the array. It returns &lt;strong&gt;&lt;em&gt;-1&lt;/em&gt;&lt;/strong&gt; if the element is not found.&lt;br&gt;
Eg:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numberArray = [1,2,3,4];
const index = numberArray.indexOf(3);
console.log(index) =&amp;gt; 2

const noIndex = numberArray.indexOf(9);
console.log(noIndex) =&amp;gt; -1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  lastIndexOf()
&lt;/h2&gt;

&lt;p&gt;The lastIndexOf() method of the array returns the last index or position at which an element can be found in an array. The array is searched backwards.&lt;br&gt;
Eg:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numberArray = [1,2,3,4,5,7,3,6,9];
const lastIndex = numberArray.lastIndexOf(3);
console.log(lastIndex ) =&amp;gt; 6

const noLastIndex = numberArray.lastIndexOf(90);
console.log(noLastIndex ) =&amp;gt; -1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Now, let's go into the business of the day.&lt;br&gt;
&lt;strong&gt;NOTE&lt;/strong&gt;: &lt;em&gt;The purpose of this tutorial is not to make an array of repeated values a unique array, rather it is to pick elements that appear only once in a repeated array.&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Example&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Given an array of repeated values:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const repeatedVals =  [5, 7, 3, 9, 4, 9, 8, 3, 2, 7, 2];&lt;/code&gt;&lt;br&gt;
We are going to output a new array of unique values.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function uniqueValues(arr) {
  const uniqueVals = [];

  return uniqueVals
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation: we created a function, inside the function, we created an empty array to hold the unique values and then return that new array. Next, we are going to write our for-loop and some conditions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// [5, 7, 3, 9, 4, 9, 8, 3, 2, 7, 2];

function uniqueValues(arr) {
  const uniqueVals = [];
  const lenOfArr = arr.length;

  for (let i = 0; i &amp;lt; lenOfArr; i++){
    const val = arr[i];
    if (arr.lastIndexOf(val) === arr.indexOf(val)) {
      uniqueVals.push(val)
    }
  }
  return uniqueVals
}
uniqueValues(repeatedVals)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Output-
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;[5, 4, 8]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;br&gt;
The &lt;code&gt;if&lt;/code&gt; conditional simply checks if the returned indices of both methods are equal. In the case where they are not equal, it simply means that the current value has a duplicate in the array.&lt;/p&gt;

&lt;p&gt;Thank you for checking out this post. In case of any errors or suggestions, kindly notify me.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>array</category>
      <category>tutorial</category>
      <category>uniqueelement</category>
    </item>
  </channel>
</rss>
