<?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: Esco Obong</title>
    <description>The latest articles on DEV Community by Esco Obong (@esco).</description>
    <link>https://dev.to/esco</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%2F359602%2F2aa55baf-5067-4abc-8b1e-e71044c274dc.jpeg</url>
      <title>DEV Community: Esco Obong</title>
      <link>https://dev.to/esco</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/esco"/>
    <language>en</language>
    <item>
      <title>Solution &amp; Explanation: Leetcode 30 Day Challenge Day 1 - Single Number</title>
      <dc:creator>Esco Obong</dc:creator>
      <pubDate>Thu, 02 Apr 2020 14:53:06 +0000</pubDate>
      <link>https://dev.to/esco/leetcode-30-day-challange-day-1-single-number-1hd8</link>
      <guid>https://dev.to/esco/leetcode-30-day-challange-day-1-single-number-1hd8</guid>
      <description>&lt;h2&gt;
  
  
  Challenge
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://leetcode.com/explore/featured/card/30-day-leetcoding-challenge/"&gt;https://leetcode.com/explore/featured/card/30-day-leetcoding-challenge/&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Question
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://leetcode.com/problems/single-number/"&gt;https://leetcode.com/problems/single-number/&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
 * @param {number[]} nums
 * @return {number}
 */&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;singleNumber&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;^=&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;  
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Explanation
&lt;/h2&gt;

&lt;p&gt;This solution uses the properties of the XOR operation to find the number by XOR'ing all of them together. With XOR the rule is that the result is &lt;code&gt;1&lt;/code&gt; if the numbers are different and &lt;code&gt;0&lt;/code&gt; if they’re the same. The XOR operator is &lt;code&gt;^&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 ^ 1 = 0 (same 1s, leads to 0)
0 ^ 0 = 0 (same 0s, leads to 0)
1 ^ 0 = 1 (different 1 and 0 leads to 1)
0 ^ 1 = 1 (different 1 and 0 leads to 1)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Since XOR operates on the binary representation of a number, you can see the effect of doing XOR on two 2s will result in 0.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;2&lt;/code&gt; in binary is &lt;code&gt;010&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;2 ^ 2&lt;/code&gt; is the same as &lt;code&gt;010 ^ 010&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If we lay it out like a multiplication problem its easier to see how the bits align. The form is similar to multiplication, addition, subtraction..etc except the operation is XOR. Similar concept, but different operation.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2 + 2 = 4 is equivalent to

2 +
2
—
4
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2 ^ 2 is equivalent to
010 ^
010
——
?
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Where ? Is the answer. If we work through this problem by looking at the top and bottom numbers from right to left (just like multiplication, addition, subtraction..etc) we get &lt;code&gt;000&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0 ^ 0 = 0
1 ^ 1 = 0
0 ^ 0 = 0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;010 ^
010
——
000
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Since the top and bottom are the same number, each position will contain the same bit (0 or 1), we get 0 based on the rule mentioned above “0 if they’re the same”. Because of this, we get 0 if we XOR any number by itself. Another example is &lt;code&gt;4 ^ 4&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;100 ^
100
——
?
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0^0=0
0^0=0
1^1=0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;100 ^
100
——
000
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Each time you XOR a number with itself, it “zeroes out” the number. When working with decimals this happens under the hood too. The trick for this question is realizing that this “zeroing” out will happen even if you XOR another number beforehand. For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;2 ^ 1 ^ 2 = 1&lt;/code&gt; because the &lt;code&gt;2 ^ 2&lt;/code&gt; “zero” each other out&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;010 ^
001 ^
010
——
?
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0^1 = 1, 1 ^ 0 = 1
1^0 = 1, 1 ^ 1 = 0
0^0 = 0, 0^0 = 0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;010
001
010
——
001 &amp;lt;— only the 1 remains because the 2s (010) canceled each other out after the XOR
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;With this in mind you can see how you can identify a single missing number because all other pairs of numbers will “zero” each other out, just as they did in the 2 ^ 1 ^ 2 example.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;7 ^ 3 ^ 9 ^ 3 ^ 7 ^ 4 ^ 4 = 9&lt;/code&gt; because the 7s, 3s, and 4s zero each other out.&lt;/p&gt;

</description>
      <category>leetcode</category>
      <category>computerscience</category>
      <category>algorithms</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
