<?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: Ashwin</title>
    <description>The latest articles on DEV Community by Ashwin (@ashking).</description>
    <link>https://dev.to/ashking</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%2F64986%2Fb198f2c6-a4eb-4e83-8818-1180cbd37d3c.jpeg</url>
      <title>DEV Community: Ashwin</title>
      <link>https://dev.to/ashking</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ashking"/>
    <language>en</language>
    <item>
      <title>Find the Duplicate Number</title>
      <dc:creator>Ashwin</dc:creator>
      <pubDate>Thu, 13 Aug 2020 15:09:28 +0000</pubDate>
      <link>https://dev.to/ashking/find-the-duplicate-number-4ni7</link>
      <guid>https://dev.to/ashking/find-the-duplicate-number-4ni7</guid>
      <description>&lt;p&gt;I was given a challenge by a friend to find the duplicates in an array. &lt;/p&gt;

&lt;p&gt;For instance, a function when it receives an array -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[1, 2, 3, 4, 5, 2, 2, 2]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;should return the duplicated number &lt;code&gt;2&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;He challenged me to write the code without using more than 1 loop. &lt;/p&gt;

&lt;p&gt;My first try&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const findDuplicate = nums =&amp;gt; {

    const uniqueSet = []
    let duplicate = null
    nums.map(x =&amp;gt; {
        if(uniqueSet.includes(x)){
           duplicate = x 
        }
        uniqueSet.push(x)
    })
    return duplicate
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;so, &lt;code&gt;findDuplicate([1, 2, 3, 4, 5, 2, 2, 2])&lt;/code&gt; returns &lt;code&gt;2&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;He was not satisfied but wanted to try differently rather than looping it through. &lt;/p&gt;

&lt;p&gt;After hours of breaking my head, I came up with this -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const findDuplicate = nums =&amp;gt; {

    const uniqueSet = new Set(nums)
    const arrSum = nums.reduce((a, b) =&amp;gt; a + b)

    let uniqueArray = Array.from(uniqueSet)
    const uniqueSetSum = uniqueArray.reduce((a, b) =&amp;gt; a + b)
    const duplicate = Math.abs(arrSum - uniqueSetSum) / (nums.length - uniqueArray.length)

    return duplicate
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;He did not understand what is going on at the first look. After a while, he understood the code. &lt;/p&gt;

&lt;p&gt;Which one do you prefer? Simpler code or Complex code? Or is there any other way you would solve this? &lt;/p&gt;

&lt;p&gt;Share your thoughts. Thanks. &lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
  </channel>
</rss>
