<?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: Bryan Algutria</title>
    <description>The latest articles on DEV Community by Bryan Algutria (@bryanalgutria).</description>
    <link>https://dev.to/bryanalgutria</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%2F1311695%2F30ecce31-5a9a-493a-b823-266537de0498.png</url>
      <title>DEV Community: Bryan Algutria</title>
      <link>https://dev.to/bryanalgutria</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bryanalgutria"/>
    <language>en</language>
    <item>
      <title>Unlocking the XOR Operator in Go: Finding the Single Non-Duplicate Element</title>
      <dc:creator>Bryan Algutria</dc:creator>
      <pubDate>Mon, 22 Jul 2024 15:51:32 +0000</pubDate>
      <link>https://dev.to/bryanalgutria/unlocking-the-xor-operator-in-go-finding-the-single-non-duplicate-element-2npb</link>
      <guid>https://dev.to/bryanalgutria/unlocking-the-xor-operator-in-go-finding-the-single-non-duplicate-element-2npb</guid>
      <description>&lt;p&gt;In software development, understanding the fundamentals allows you to navigate the depths of this career. Bitwise operations can unlock powerful techniques for optimizing code and solving complex problems. Recently I was solving a simple problem, where I needed to use the XOR (exclusive OR) operator, and I found it particularly fascinating. Let me introduce you into how the XOR operator works and see it in action with a practical example: finding the single non-duplicate element in an array.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is XOR?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The XOR operator, denoted by &lt;code&gt;^&lt;/code&gt; in Go, compares bits of two integers. For each bit position, the result is &lt;code&gt;1&lt;/code&gt; if the bits are different and &lt;code&gt;0&lt;/code&gt; if they are the same. Here’s the truth table for XOR:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;A&lt;/th&gt;
&lt;th&gt;B&lt;/th&gt;
&lt;th&gt;A ^ B&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;This property makes XOR a powerful tool for various tasks, including swapping variables, checking equality, and more.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solving the Single Non-Duplicate Problem&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Imagine you have an array where every element appears twice except for one. Our task is to find this single non-duplicate element efficiently. Traditional methods might involve extra space or complex loops, but XOR simplifies this task significantly.&lt;/p&gt;

&lt;p&gt;Here’s how you can do it in Go:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;singleNonDuplicate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;list&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;element&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;range&lt;/span&gt; &lt;span class="n"&gt;list&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;^=&lt;/span&gt; &lt;span class="n"&gt;element&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What happened?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It started with &lt;code&gt;result&lt;/code&gt; set to &lt;code&gt;0&lt;/code&gt;. Then for each number in the array, XOR it with &lt;code&gt;result&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Properties of XOR:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;x ^ x = 0&lt;/code&gt;: XORing a number with itself results in &lt;code&gt;0&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;x ^ 0 = x&lt;/code&gt;: XORing a number with &lt;code&gt;0&lt;/code&gt; leaves it unchanged.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;x ^ z = ?&lt;/code&gt;: XORing a number with other number different than zero?? Let's operate: &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To do this, we need to represent each number in bits.&lt;/p&gt;

&lt;p&gt;0 0 0 0 0 1 0 0 = 4&lt;br&gt;
0 0 0 0 0 0 0 1 = 1&lt;br&gt;
&lt;u&gt;_________________&lt;/u&gt;&lt;br&gt;
&lt;strong&gt;0 0 0 0 0 1 0 1 = 5&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;After XORing each bit of each number, we got a result. Now, let's apply this in a list of numbers to find the unique value.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Consider the array &lt;code&gt;[4, 1, 2, 1, 2]&lt;/code&gt;. Let’s trace the XOR operations step by step:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Start with &lt;code&gt;result = 0&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;result = 0 ^ 4 = 4&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;result = 4 ^ 1 = 5&lt;/code&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;result = 5 ^ 2 = 7&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;result = 7 ^ 1 = 6&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;result = 6 ^ 2 = 4&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After processing all numbers, &lt;code&gt;result&lt;/code&gt; contains the single non-duplicate element. This is because all pairs cancel each other out. The final &lt;code&gt;result&lt;/code&gt; is &lt;code&gt;4&lt;/code&gt;, which is the single non-duplicate element.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why Use XOR?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Efficiency&lt;/strong&gt;: XOR operations are fast and require constant time, O(n).&lt;br&gt;
&lt;strong&gt;Simplicity&lt;/strong&gt;: No need for extra space or complicated logic.&lt;br&gt;
&lt;strong&gt;Elegant Solution&lt;/strong&gt;: The XOR property provides a clean and straightforward solution.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The XOR operator is a powerful tool in a programmer’s toolkit, especially for problems involving bit manipulation. By exploiting its properties, you can write concise and efficient code, as demonstrated with the single non-duplicate problem. Give it a try in your next project and see how XOR can simplify your algorithms.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
