<?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: gbenga fagbola</title>
    <description>The latest articles on DEV Community by gbenga fagbola (@clouded_knight).</description>
    <link>https://dev.to/clouded_knight</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%2F827970%2Fbe86d206-01ce-401c-be1c-c7294f65b120.jpg</url>
      <title>DEV Community: gbenga fagbola</title>
      <link>https://dev.to/clouded_knight</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/clouded_knight"/>
    <language>en</language>
    <item>
      <title>Building a Distributed Search Engine in Go</title>
      <dc:creator>gbenga fagbola</dc:creator>
      <pubDate>Fri, 31 Jan 2025 18:25:15 +0000</pubDate>
      <link>https://dev.to/clouded_knight/building-a-distributed-search-engine-in-go-561f</link>
      <guid>https://dev.to/clouded_knight/building-a-distributed-search-engine-in-go-561f</guid>
      <description>&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;Searching through large datasets efficiently is a common problem in distributed systems. This article demonstrates how to build a distributed search engine using Go’s concurrency features, allowing multiple workers to process and search data simultaneously.&lt;/p&gt;

&lt;h3&gt;
  
  
  Understanding the Concept
&lt;/h3&gt;

&lt;p&gt;A distributed search engine divides the dataset into chunks, assigns each chunk to a worker, and runs multiple workers in parallel to perform the search. This improves performance by reducing the time needed to find a match, especially for large datasets.&lt;/p&gt;

&lt;p&gt;In our example, we will:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Simulate a distributed search across a user database.&lt;/li&gt;
&lt;li&gt;Use multiple worker goroutines to search concurrently.&lt;/li&gt;
&lt;li&gt;Use channels for communication between workers and the main process.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Code Implementation
&lt;/h3&gt;

&lt;p&gt;Below is the complete Go implementation of a distributed search engine:&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;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"log"&lt;/span&gt;
    &lt;span class="s"&gt;"os"&lt;/span&gt;
    &lt;span class="s"&gt;"strings"&lt;/span&gt;
    &lt;span class="s"&gt;"time"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c"&gt;// User struct represents a user with an email and name&lt;/span&gt;
&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Email&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
    &lt;span class="n"&gt;Name&lt;/span&gt;  &lt;span class="kt"&gt;string&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;// DataBase is a slice of User representing stored user data&lt;/span&gt;
&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;DataBase&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;Email&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"adebayo.olu@example.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Adebayo Olu"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;Email&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"chioma.okafor@example.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Chioma Okafor"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;Email&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"ibrahim.abubakar@example.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Ibrahim Abubakar"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;Email&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"ngozi.uchenna@example.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Ngozi Uchenna"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;Email&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"chinedu.ekene@example.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Chinedu Ekene"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;Email&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"toyin.adebayo@example.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Toyin Adebayo"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;Email&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"uche.nwosu@example.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Uche Nwosu"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;Email&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"bola.johnson@example.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Bola Johnson"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;Email&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"femi.ogunleye@example.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Femi Ogunleye"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;Email&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"damilola.ogun@example.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Damilola Ogun"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;Email&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"amaka.nnamdi@example.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Amaka Nnamdi"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;Email&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"segun.olawale@example.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Segun Olawale"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;Email&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"bisi.adewale@example.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Bisi Adewale"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;Email&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"kunle.akintola@example.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Kunle Akintola"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;Email&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"funke.adebisi@example.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Funke Adebisi"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;Email&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"nkechi.okonkwo@example.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Nkechi Okonkwo"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;// Worker struct represents a worker that processes a subset of users&lt;/span&gt;
&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Worker&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="n"&gt;User&lt;/span&gt;  &lt;span class="c"&gt;// Slice of users assigned to the worker&lt;/span&gt;
    &lt;span class="n"&gt;ch&lt;/span&gt;    &lt;span class="k"&gt;chan&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;User&lt;/span&gt;  &lt;span class="c"&gt;// Channel for sending found users&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt;  &lt;span class="kt"&gt;string&lt;/span&gt;  &lt;span class="c"&gt;// Worker name identifier&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;// NewWorker initializes a new Worker instance&lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;NewWorker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ch&lt;/span&gt; &lt;span class="k"&gt;chan&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;Worker&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;Worker&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ch&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ch&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;// Find searches for a user email within the worker's assigned users&lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;Worker&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;Find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&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;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;strings&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Contains&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c"&gt;// Check if email contains search term&lt;/span&gt;
            &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ch&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="c"&gt;// Send found user to the channel&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;// DistributeWorkload creates workers dynamically based on database size&lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;DistributeWorkload&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ch&lt;/span&gt; &lt;span class="k"&gt;chan&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;numWorkers&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt; &lt;span class="c"&gt;// Define number of workers&lt;/span&gt;
    &lt;span class="n"&gt;batchSize&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;numWorkers&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;numWorkers&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;batchSize&lt;/span&gt;
        &lt;span class="n"&gt;end&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;batchSize&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;numWorkers&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;end&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// Assign remaining users to last worker&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;go&lt;/span&gt; &lt;span class="n"&gt;NewWorker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;ch&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Worker"&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Usage: go run main.go &amp;lt;email&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c"&gt;// Get the email argument from command line&lt;/span&gt;
    &lt;span class="n"&gt;ch&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nb"&gt;make&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;chan&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// Create a channel for user search results&lt;/span&gt;

    &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Looking for %s"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c"&gt;// Distribute workload dynamically&lt;/span&gt;
    &lt;span class="n"&gt;DistributeWorkload&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;DataBase&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ch&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c"&gt;// Listen for user results or timeout if no result is found&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt;&lt;span class="n"&gt;ch&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The email is %s owned by %s"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;After&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;100&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Millisecond&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The email %s was not found"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="c"&gt;// Exit loop after timeout&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Explanation of the Code
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;User Data and Database:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We define a &lt;code&gt;User&lt;/code&gt; struct containing &lt;code&gt;Email&lt;/code&gt; and &lt;code&gt;Name&lt;/code&gt; fields.&lt;/li&gt;
&lt;li&gt;A slice of &lt;code&gt;User&lt;/code&gt; acts as our database.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Worker Struct and Methods:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Each &lt;code&gt;Worker&lt;/code&gt; processes a subset of the database.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;Find&lt;/code&gt; method checks if an email contains the search term and sends results to a channel.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Concurrency Using Goroutines:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The dataset is split among three workers.&lt;/li&gt;
&lt;li&gt;Each worker runs concurrently to search for the provided email.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Communication with Channels:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A &lt;code&gt;channel&lt;/code&gt; is used to send found users from workers to the main function.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;select&lt;/code&gt; statement listens for results or a timeout.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Timeout Mechanism:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If no results are received within &lt;code&gt;100ms&lt;/code&gt;, the search terminates with a “not found” message.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Running the Code
&lt;/h3&gt;

&lt;p&gt;To run the program, execute:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;go run main.go miller
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will search for emails containing “miller” and return matching results.&lt;/p&gt;

&lt;h3&gt;
  
  
  Benefits of This Approach
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Parallel Processing:&lt;/strong&gt; Multiple workers search concurrently, reducing response time.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scalability:&lt;/strong&gt; More workers can be added to handle larger datasets.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Efficiency:&lt;/strong&gt; The timeout prevents unnecessary waiting.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;This article demonstrated how to build a simple distributed search engine using Go. By leveraging goroutines and channels, we efficiently parallelized the search process, making it scalable for larger datasets. Future improvements could include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Using a distributed architecture like Apache Kafka or Elasticsearch.&lt;/li&gt;
&lt;li&gt;Implementing an indexing mechanism for faster searches.&lt;/li&gt;
&lt;li&gt;Running the system across multiple machines for even better performance.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let me know your thoughts and how you’d optimize this approach further!&lt;/p&gt;

</description>
      <category>go</category>
      <category>distributedsystems</category>
      <category>parallelism</category>
      <category>architecture</category>
    </item>
    <item>
      <title>Multiple Pointers</title>
      <dc:creator>gbenga fagbola</dc:creator>
      <pubDate>Mon, 02 May 2022 13:30:07 +0000</pubDate>
      <link>https://dev.to/clouded_knight/multiple-pointers-592h</link>
      <guid>https://dev.to/clouded_knight/multiple-pointers-592h</guid>
      <description>&lt;p&gt;The idea of the multiple pointer pattern revolves solely around creating identifiers or pointers that correspond to an index or position, which moves towards a specific direction based on the condition set.&lt;/p&gt;

&lt;p&gt;This pattern is quite efficient for solving problems with minimal space complexity. &lt;/p&gt;

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

&lt;p&gt;Write a function that accepts a sorted array of integers. The Function in question should find the first pair of integers where the sum is 0, the expected output should return the pair of numbers whose value summed up to &lt;code&gt;0&lt;/code&gt; and &lt;code&gt;undefined&lt;/code&gt; if the pair does not exit.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function sumZero(arr) {

    let left = 0;
    let right = arr.length -1;

    while(left &amp;lt; right){
        let sum = arr[left] + arr[right];

        if(sum === 0){
            return [arr[left], arr[right]];
        } else if(sum &amp;lt; 0){
            left++
        } else if(sum &amp;gt; 0){
            right--
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;sumZero([-3, -2, -1, 0, 1, 2, 4])&lt;/strong&gt;&lt;br&gt;
returns &lt;code&gt;[-2, 2]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;sumZero([-4,2,1])&lt;/strong&gt; &lt;br&gt;
 returns &lt;code&gt;undefined&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;From the above block of code, we have a variable declared for pointers left and right. Such that the left pointer sweeps through the array from the first item in the list towards the ending of the list. While the right pointer starts from the end of the list and moves towards the beginning of the list. All this happens provided the left identifier wouldn't overlap with that of the right.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Frequency Counter</title>
      <dc:creator>gbenga fagbola</dc:creator>
      <pubDate>Thu, 14 Apr 2022 18:44:59 +0000</pubDate>
      <link>https://dev.to/clouded_knight/frequency-counter-hl0</link>
      <guid>https://dev.to/clouded_knight/frequency-counter-hl0</guid>
      <description>&lt;p&gt;This pattern employs JavaScript Object to store the frequency or rather the occurrence of data and is most useful in data comparison.&lt;/p&gt;

&lt;p&gt;Let's jump right in with an example.&lt;/p&gt;

&lt;p&gt;Write a function named similar such that it accepts two (2) arrays. The intended function should return true if every value in array one has its corresponding value squared in array two.&lt;/p&gt;

&lt;p&gt;such as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;similar([1,2,3],[4,1,9])   this would return true
similar([1,2,1],[4,4,1])   this would return false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Making use of the Frequency Counter Approach Below:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function similar(array1, array2) {

    if (array1.length !== array2.length){
        return false;    
    }

    let freqCounter1 = {};
    let freqCounter2 = {};

    for(let val of array1){
        freqCounter1[val] = (freqCounter1[val] || 0) +1      
    }

    for(let val of array2){
        freqCounter2[val] = (freqCounter2[val] || 0) +1      
    }


    for(let key in freqCounter1){

        if(!(key ** 2 in freqCounter2)){
            return false
        }

        if(freqCounter2[key ** 2] !== freqCounter1[key]) {
            return false;
        }
    }

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Test Case&lt;/strong&gt;: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;similar([1,2,3,2,5],[4,9,4,1]) returns &lt;code&gt;false&lt;/code&gt;&lt;br&gt;
similar([1,2,3],[9,1,4]) returns &lt;code&gt;true&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Explanation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So let's take things in a systematic approach. Feel free to employ the intended approach we went through in the last lesson to solve the problem set yourself.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Function Declaration:&lt;/strong&gt; declaration of a function named &lt;code&gt;similar&lt;/code&gt; which would take a set of two distinct arrays as an input.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Edge Case:&lt;/strong&gt; &lt;code&gt;the first block of code after the function declaration.&lt;/code&gt; This can be referred to as a short circuit whereby it runs before the core algorithms logic and returns a set result in case of invalid or undesired input.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;In this case, check if the length of the first array is not equal to that of the second array and returns an output. In this case, it just returns a false statement. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Objects declaration:&lt;/strong&gt; you can say this is where the core logic of this approach relies on, but note it doesn’t necessarily have to be an object, such that it can also be an array, although the Big O of Object states that we have a constant rate of accessing, removing, and adding an item into it, which makes it a much-optimized choice. 
Empty Objects were declared to store the frequency of each character in the array respectively as named above. &lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;The same goes for freqCounter2.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Core Objective:&lt;/strong&gt; now let's focus on the core objective, which is to look for the presence of the square of each character in array1 within array2. From the block of code &lt;code&gt;for(let key in freqCounter1){&lt;/code&gt; we loop through each key within freqCounter1 in other to make comparisons, such as this resulting block of code  &lt;code&gt;if(!(key ** 2 in freqCounter2)){&lt;/code&gt; which acts as a short circuit which checks if the square of each key in &lt;strong&gt;freqCounter1&lt;/strong&gt; is not present in &lt;strong&gt;freqCounter2&lt;/strong&gt;, and return false.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Final Piece:&lt;/strong&gt; Now the final block of code is aimed at checking if the square of each element in &lt;strong&gt;freqCounter2&lt;/strong&gt; has a corresponding equal within &lt;strong&gt;freqCounter1&lt;/strong&gt;. If any is found wanting 😩, its evaluates to false, and if not we arrive at the end of the code block where our code &lt;code&gt;retuns true;&lt;/code&gt;if all conditions were passed.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




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

&lt;p&gt;Given two strings, write a function to determine if the second string is an anagram of the first. an anagram is a word, phrase, or name formed by rearranging the letters of another, such as &lt;code&gt;RAT is an anagram of TAR&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function anagrams(first, second) {

    if(first.length !== second.length){
        return false;
    }


    let freqCounter1 = {};
    let freqCounter2 = {};

    for(let val of first){
        freqCounter1[val] = (freqCounter1[val] || 0) +1
    }

    for(let val of second){
        freqCounter2[val] = (freqCounter2[val] || 0) +1
    }

    console.log(freqCounter1);
    console.log(freqCounter2);

    for(let key in freqCounter1){
        if(!(freqCounter2.hasOwnProperty(key))){
            return false
        }

        if(freqCounter1.values !== freqCounter2.values){
            return false;
        }
    }

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Test Case&lt;/strong&gt;: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;similar(bat, tab) returns &lt;code&gt;true&lt;/code&gt;&lt;br&gt;
similar(salt, tale) returns &lt;code&gt;false&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Final Example&lt;/strong&gt;&lt;br&gt;
Count unique Integers in a List&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function countUnique(arr) {

    let counter = {};
    let count = [];

    for(let val of arr){

        counter[val] = (counter[val] || 0) +1;     
    }


    for(let key in counter){
        count.push(key)
    }

    return count.length;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;countUnique([-2,-1,-3,1])&lt;/strong&gt;&lt;br&gt;
returns &lt;code&gt;4&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I hope the above examples shed light on the conceptualized pattern discussed. See you in the next lecture.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>algorithms</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Problem solving Patterns</title>
      <dc:creator>gbenga fagbola</dc:creator>
      <pubDate>Tue, 05 Apr 2022 14:55:14 +0000</pubDate>
      <link>https://dev.to/clouded_knight/problem-solving-patterns-2iib</link>
      <guid>https://dev.to/clouded_knight/problem-solving-patterns-2iib</guid>
      <description>&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h6&gt;
  
  
  This is an introduction to common problem solving pattern
&lt;/h6&gt;

&lt;p&gt;Hey Guys 😊,&lt;/p&gt;

&lt;p&gt;So as discussed in the previous lecture, we would be looking at common problem solving patterns we can implement when faced with a problem set. So let’s jump right in.&lt;/p&gt;

&lt;p&gt;Let me go through the intended approach before making a list of patterns I hope to go through.&lt;/p&gt;




&lt;blockquote&gt;
&lt;p&gt;&lt;u&gt;&lt;strong&gt;How to Improve in Problem Solving&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;(i). &lt;strong&gt;Develop a strategic Plan:&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;I admit it can be really tempting to jump right into solving a problem set with little or no understanding, with hopes of navigating through it. But I propose you devise a sound enough plan for accomplishing such a task.&lt;/p&gt;

&lt;p&gt;(ii). &lt;strong&gt;The mastery of common problem-solving patterns:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Of course, this would give you a grasp on the most common form which various problem sets might take. Thereby giving you foresight towards tackling these said problem sets.&lt;/p&gt;




&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Let Highlight the Problem-Solving Process&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ol&gt;
&lt;li&gt;Understand the Problem.&lt;/li&gt;
&lt;li&gt;Explore Concrete Examples.&lt;/li&gt;
&lt;li&gt;Break the Problem-set down.&lt;/li&gt;
&lt;li&gt;Simplify - make a proposed solution.&lt;/li&gt;
&lt;li&gt;Implement the solution to various case study.&lt;/li&gt;
&lt;li&gt;Refactor  - optimise the solution.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let now take a deeper look into the highlight above&lt;/p&gt;




&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Understanding the Problem set&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ol&gt;
&lt;li&gt;You should try to restate the essence of the problem set according to your understanding.&lt;/li&gt;
&lt;li&gt;Take a clear look at the intended input &amp;amp; expected output. Keeping in mind possible edge cases.&lt;/li&gt;
&lt;li&gt;Determine if the expected output can be determined from the input.&lt;/li&gt;
&lt;li&gt;Have a final glance through and ensure you have taken note of every important detail.&lt;/li&gt;
&lt;/ol&gt;




&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Explore Concrete Examples&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ol&gt;
&lt;li&gt;Look through every possible edge case, such as invalid input, the presence of whitespace or special characters etcetera in other to devise a solid algorithm.&lt;/li&gt;
&lt;li&gt;Make users' Stories to aid in a graphical are relatable presentation. Which can show a profound level of understanding and also help others understand the problem set and your intended solution.&lt;/li&gt;
&lt;li&gt;Make simple examples, then complex ones to show mastery.&lt;/li&gt;
&lt;/ol&gt;




&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Breaking things down&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There are quite a few ways to break down the problem set (the algorithmic way) such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Sudo code&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Flow diagram et Cetera.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Simplifying the Problem Sets&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;After following through the above steps, it is only expected that we get to finally simplify the problem and deduce a solution that would suffice.&lt;/p&gt;




&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Implementation of the said Solution&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We should ensure our solution passes all intended test cases, both easy and challenging. While envisaging possibilities of inconsistent input that might arise and possible ways out.&lt;/p&gt;




&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Having a Look-Back.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I employ you to create the habit of having a much-optimized version of your solution. Answering the questions&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;If there is the expected outcome&lt;/li&gt;
&lt;li&gt;Checking out other possible approaches &lt;/li&gt;
&lt;li&gt;How understandably is the solution (which refers us to the criteria we spoke previously about)&lt;/li&gt;
&lt;li&gt;It adaption to a similar problem set&lt;/li&gt;
&lt;li&gt;Can the Performance of the Solution be Improved? (in terms of time &amp;amp; Space Complexity)&lt;/li&gt;
&lt;li&gt;Check if it follows basic conventions and standards.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;code&gt;Example&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Let's Go through a Simple Problem set and implement the above steps. You can try this yourself, and kindly note that we are bound to have different answers but a similar approach.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Question&lt;/strong&gt; Write a function that takes a string and returns the frequency of each character in the given &lt;strong&gt;String&lt;/strong&gt;, considering the laid down principles above.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;From 1:&lt;/code&gt; &lt;strong&gt;Understanding the Problem&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;In simple words, return the occurrence of each character in a given string. &lt;/li&gt;
&lt;li&gt;We can deduce a string as the intended input.&lt;/li&gt;
&lt;li&gt;Yes, the expected Output can be gotten from the input.&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;&lt;code&gt;From 2:&lt;/code&gt; &lt;strong&gt;Exploring concrete examples&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Ben was given the assignment to count the re-occurring characters in a list of countries. So he got fancy and employed an automated process. Via an algorithmic approach.&lt;/p&gt;




&lt;p&gt;&lt;code&gt;From 3:&lt;/code&gt; &lt;strong&gt;Breaking things down&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Defining the function &lt;/li&gt;
&lt;li&gt;Instantiation of a counter&lt;/li&gt;
&lt;li&gt;Examination of every character in the input &lt;/li&gt;
&lt;li&gt;Looking out for keen details such as similarities and count&lt;/li&gt;
&lt;li&gt;Return the output or statement based on expected edge cases.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;code&gt;From 4:&lt;/code&gt; &lt;strong&gt;Simplifying the problem set&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function countCharacter(arr){

    let countResult = {};  

    for(let i = 0; i &amp;lt; arr.length; i++){       
        let countIndex = arr[i].toLowerCase();       
        if(countResult[countIndex] &amp;gt; 0){       
            countResult[countIndex]++;    
        } else {     
            countResult[countIndex] = 1;       
        }
    }
    return countResult;
}

countCharacter("hey guy!")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;result&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;{" ": 1, !: 1, e: 1, g: 1, h: 1, u: 1, y: 2}&lt;/code&gt;&lt;/p&gt;



&lt;p&gt;&lt;code&gt;from 5:&lt;/code&gt; &lt;strong&gt;Contemplate Edge-Cases&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function countCharacter(arr){
    let countResult = {};  

    for(var char of arr){                
        char = char.toLowerCase();
        if(/[a-z0-9]/.test(char)){
            if(countResult[char] &amp;gt; 0){
                countResult[char]++
            } else {
                countResult[char] = 1;
            }
        }
    }
    return  countResult;
}

countCharacter("hey guy!")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;result&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;{h: 1, e: 1, y: 2, g: 1, u: 1}&lt;/code&gt;&lt;/p&gt;



&lt;p&gt;&lt;code&gt;from 6:&lt;/code&gt; &lt;strong&gt;Refactoring - Looking Back&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function countCharacter(arr){
    let countResult = {};  

    for(var char of arr){                
        char = char.toLowerCase();
        if(/[a-z0-9]/.test(char)){
        countResult[char] = (countResult[char] || 0) +1;     
        }
    }
    return  countResult;
}

countCharacter("hey guy!")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;result&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;{h: 1, e: 1, y: 2, g: 1, u: 1}&lt;/code&gt;&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;, you can easily run the code blocks above in your &lt;a href="https://developer.chrome.com/docs/devtools/javascript/snippets/#:~:text=%23%20Create%20a%20Snippet%20through%20the%20Command%20Menu&amp;amp;text=inside%20of%20DevTools.-,Press%20Control%2BShift%2BP%20or%20Command%2BShift%2BP,Enter%20to%20run%20the%20command."&gt;chrome snippet&lt;/a&gt; to see the respective results yourself.&lt;/p&gt;

&lt;p&gt;The above code snippet is a solution to the problem statement above, and do not worry if you don’t understand. I would start explaining line by line each code block I write from the forthcoming lectures as this is to help you understand the underlying concept first.&lt;/p&gt;

&lt;p&gt;See you in the next part 👏🏻.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>algorithms</category>
      <category>tutorial</category>
      <category>interview</category>
    </item>
    <item>
      <title>Algorithm &amp; Data Structure</title>
      <dc:creator>gbenga fagbola</dc:creator>
      <pubDate>Tue, 29 Mar 2022 13:48:52 +0000</pubDate>
      <link>https://dev.to/clouded_knight/algorithm-data-structure-59e0</link>
      <guid>https://dev.to/clouded_knight/algorithm-data-structure-59e0</guid>
      <description>&lt;blockquote&gt;
&lt;h6&gt;
  
  
  &lt;strong&gt;This is a beginners guide towards understanding &amp;amp; mastery of data structure &amp;amp; algorithm using Javascript&lt;/strong&gt;.
&lt;/h6&gt;
&lt;/blockquote&gt;

&lt;p&gt;In a quick and well-detailed series of lectures, I would be taking you on a quest of disintegrating Algorithms &amp;amp; Data-Structure in JavaScript, which is solemnly aimed at beginners.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prerequisites&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Basic Knowledge of JavaScript &lt;/li&gt;
&lt;li&gt;Patience and Time to Read Though&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;And for those who might have the question “&lt;strong&gt;Is a functional System required?&lt;/strong&gt;” well for this stage, I would say an emphatic No and advice you to follow through, even if you make use of a pen and a piece of paper.&lt;/p&gt;

&lt;p&gt;The overall concept and ability to tackle challenges should be your key takeaway from this tutorial.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;So let's get started.&lt;/strong&gt;&lt;br&gt;
 In this part of the series, I would be taking a look at the introduction to Data-Structure &amp;amp; Algorithm, and of course, its importance, Big O Notation, Object &amp;amp; Arrays in JavaScript, and finally introduce known and unknown Problem-Solving Patterns.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Data Structure &amp;amp; Algorithms&lt;/strong&gt;
&lt;/h2&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;Algorithms&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;In computer programming terms, an algorithm in its' basic term refers to a set of well-defined instructions or processes aimed at solving a particular problem or accomplishing a certain task.&lt;/p&gt;

&lt;p&gt;It practically takes a set of inputs and produces the desired output. For example,&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quality of an Algorithm Revolves around these key points&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Its Input and output should be clearly defined.&lt;/li&gt;
&lt;li&gt;It should be easily understandable.&lt;/li&gt;
&lt;li&gt;It should be easily applied to solve similar problem sets.&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;There's a big misconception that algorithm has to be written in a particular programming language, whereas I am here to tell you it is not necessarily the case.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;As defined above, an algorithm is a broken-down process toward solving a problem set or accomplishing a set task.&lt;/p&gt;

&lt;p&gt;Let's take, for example, writing an algorithm to add up two numbers, leaving aside any possible edge case.&lt;/p&gt;

&lt;h3&gt;
  
  
  Algorithm to Add two numbers
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Step 1: Start
Step 2: State variables for example let number1 = 5, number2 = 8.  
Step 3: Add num1 and num2; assign the result to sum to the value of num1 &amp;amp; num2.
Step 4: display - return the sum 
Step 5: Stop
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above might not be the most elaborate way, but I hope the message is passed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is the importance of an Algorithm in a real-life scenario?&lt;/strong&gt;&lt;br&gt;
To me, it simply helps complex problem-sets seem less intimidating.&lt;/p&gt;


&lt;h2&gt;
  
  
  &lt;code&gt;Data Structure&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Data structure&lt;/strong&gt; can be referred to as storage that is used to store and organize the presentation of data. It is a way of representing data so that it can be accessed and implemented efficiently.&lt;/p&gt;

&lt;p&gt;Choosing the right data Structure pattern is quite a big deal for a project's overall working schema.&lt;/p&gt;
&lt;h3&gt;
  
  
  Two Main Data Structure Categories
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1. Linear Data Structures:&lt;/strong&gt; In Linear data structures, elements are arranged in a sequence that is one after the other. But due to its structure, when implementing complex programs, It might not be the best solution.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Examples of Linear Data Structures&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Array Data Structure &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Stacked Data Structure &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Queue Data Structure &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Linked Data Structure &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2. Non-Linear Data Structures:&lt;/strong&gt; Unlike the linear data structures above, elements in non-linear data structures are not in any sequence. They are mainly arranged hierarchically.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Example of Non-Linear Data Structures&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Graph Data Structure &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Tree Data Structure &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Map Data Structure&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Importance of Knowing Data Structures&lt;/strong&gt;&lt;br&gt;
As highlighted earlier, Data Structures help you know when and how to select the best fit data structure pattern for your project or that of your company.&lt;/p&gt;

&lt;p&gt;Let's Put a pin into data structures for now, till we circle back in later series.&lt;/p&gt;


&lt;h2&gt;
  
  
  &lt;strong&gt;BIG O&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The Importance of this is quite as emphatic as it sounds 🙃.&lt;br&gt;
 &lt;strong&gt;Big O&lt;/strong&gt; can be described as a approach, or way of generalizing or rather comparison of codes and their performance.&lt;/p&gt;

&lt;p&gt;In much simpler terms, It's a way to know which algorithm approach or block of code is best by basic comparison standards.&lt;/p&gt;

&lt;p&gt;Let's take a look at 2 different solutions to the problem set I saw from an online resource.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;br&gt;
Write a function that calculates the sum of all numbers from 1 up to and including the said number &lt;strong&gt;n&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;METHOD A&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function addUp (n) {
   let total = 0;
   for (let i = 1; i &amp;lt;= n; i++){
      total += i;
   }
  return total;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;METHOD B&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; function addUp(n) {
    return n * (n + 1)/2;
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Comparing &lt;code&gt;METHOD A&lt;/code&gt; to &lt;code&gt;METHOD B&lt;/code&gt; is where &lt;strong&gt;big O&lt;/strong&gt; comes in, whereby it takes into account key criteria, which are&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Speed&lt;/strong&gt; (how fast is it)&lt;br&gt;
&lt;strong&gt;Space&lt;/strong&gt; (if it's of lesser memory intensity)&lt;br&gt;
&lt;strong&gt;Readability&lt;/strong&gt; (if it's non-ambiguous to read &amp;amp; understand.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;Summary&lt;/code&gt;&lt;br&gt;
For &lt;strong&gt;Method A&lt;/strong&gt;, the runtime of the function is solemnly dependent on how large the value of n (i.e what it has to process).&lt;br&gt;
This gives us a &lt;strong&gt;Time Complexity - Big O&lt;/strong&gt; of &lt;code&gt;O(N). ---&amp;gt; linear&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;While for** Method B*&lt;em&gt;, the big O is **constant&lt;/em&gt;* since the operation the function has to perform is restricted to basic arithmetic operations which would take the same amount of time to run no matter the size of n.&lt;/p&gt;

&lt;p&gt;giving us the &lt;strong&gt;Time complexity - Big O&lt;/strong&gt; of &lt;code&gt;O(1) ---&amp;gt; constant&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Big O gives us the ability to discuss the impact the input of a function has on its runtime.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;whereas a function of n =&amp;gt; f(n)&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;f(n) = n        linear
f(n) = n^2      quadratic
f(n) = 1        constant
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Simplifying Big O
&lt;/h2&gt;

&lt;p&gt;Let's look at various case studies and their simplified term&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1. O(2n) = O(n)
2. O(500) = O(1)
3. O(13n^2) = 0(n^2)
4. O(n + 1) = O(n)
5. O(10000n + 5) = O(n)
6. O(n^2 + 5n + 8) = O(n^2 + n) === O(n^2)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;kindly note&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Constant and Smaller terms don’t really matter&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Arithmetic Operation is Constant&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Variable assignments are constant&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Accessing elements in an array is constant &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;For a loop, the complexity of the said loop depends on the length of the loop multiplied by the complexity of what happens in the loop.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsmyp0kir6tw6qzcugbcg.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsmyp0kir6tw6qzcugbcg.jpg" alt="Big O graph"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;&lt;code&gt;Time &amp;amp; Space Complexity rule of thumb&lt;/code&gt;&lt;br&gt;
Most primitive are constant (booleans, numbers, undefined &amp;amp; null)&lt;br&gt;
Strings are linear ( O(n) depends on the length of the string)&lt;/p&gt;

&lt;p&gt;Let's check out an example to further explain space complexity.&lt;br&gt;
for instance, a function that &lt;strong&gt;triple&lt;/strong&gt; each element in an array,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function triple(arr){
   let newArray = [];
   for (let i = 0; i &amp;lt; arr.length; i++){
       newArray.push(3 * arr[i]);
   }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In summary, the length of the input array (arr) would directly impact the length of the new array.&lt;/p&gt;

&lt;p&gt;therefore giving us a &lt;code&gt;space complexity of O(n)&lt;/code&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Analyzing Performance of Array &amp;amp; Object
&lt;/h2&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;The Big O of JavaScript Object&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Objects&lt;/strong&gt; are unordered data structures that are stored in a key-value pair&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Perks&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;It is useful in cases you don't need an order&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Fast access &amp;amp; Insertion&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Insertion O(1)
Removal   O(1)
Searching O(N)
Access    O(1)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Object Methods
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Object.keys O(N)&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Object.values   O(N)&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Object.entries  O(N)&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;.hasOwnProperties O(1)&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;The Big O of JavaScript Array&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Arrays&lt;/strong&gt; are ordered data structures. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;Perk&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Useful in cases where the order is needed.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Access.   O(1)
Searching O(N)
Insertion &amp;amp; Removal both depends on the position or rather index in which the operation is to be performed. but for the last element, there are both O(1)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thats why &lt;code&gt;.push&lt;/code&gt; and &lt;code&gt;.pop&lt;/code&gt; are primarily faster than &lt;code&gt;.shift&lt;/code&gt; and &lt;code&gt;.unshift&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Some Basic Array Methods
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;.push O(1)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.pop O(1)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.shift O(N)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.unshift O(N)&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://www.w3schools.com/jsref/jsref_obj_array.asp" rel="noopener noreferrer"&gt;JavaScript Array Methods&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I would strongly advise you to visit the link above from time to time and get an in-depth knowledge of Javascript array.&lt;/p&gt;

&lt;h2&gt;
  
  
  Next Topic
&lt;/h2&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;Popular Problem Solving Pattern&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;In the coming lecture, we would have hands-on practice towards common problem-solving patterns in algorithms and at least have an idea of where to start form while tackling problem sets.&lt;/p&gt;




&lt;p&gt;My goal is not to bore you or rather impress with ambiguous words but rather convey in simple terms what the said subject matter is all about. On that note, would see you in the upcoming part.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>algorithms</category>
      <category>beginners</category>
      <category>datastructure</category>
    </item>
    <item>
      <title>React-Redux at it's Simplest!</title>
      <dc:creator>gbenga fagbola</dc:creator>
      <pubDate>Thu, 24 Mar 2022 17:20:39 +0000</pubDate>
      <link>https://dev.to/clouded_knight/react-redux-at-its-simplest-2kep</link>
      <guid>https://dev.to/clouded_knight/react-redux-at-its-simplest-2kep</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;Hey guys!&lt;/strong&gt; 👋🏽.
&lt;/h2&gt;

&lt;p&gt;Thanks for joining.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;First and foremost, my advice would be that you should always try as much as possible to understand the underlying concept of a tool, framework, language, etcetera before using - implementing it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Which makes learning worthwhile and sort of fun.&lt;/p&gt;

&lt;p&gt;After all, is said and done 🙃, let's get started. This lecture will be in different parts so as not to bore you. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;React&lt;/strong&gt; can practically be considered as a group of components that are rendered in a tree-like structure. Of which is mainly centred around User-Interface.&lt;/p&gt;

&lt;p&gt;It gives room for State Management tools to play an important role, such as Redux, Graph-Ql etcetera. Redux is a great state management tool and not just for react. Kindly note.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Concept&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;I would go on a limb here and assume everyone reading is conversant with the concept of a Component or a component-driven development. So let's take a look at &lt;em&gt;diagram 1.0&lt;/em&gt; below&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4y0osj0dq3lzs27zfs8k.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4y0osj0dq3lzs27zfs8k.jpg" alt="without redux"&gt;&lt;/a&gt;&lt;br&gt;
 This shows how each component is mapped to its respective state. &lt;/p&gt;

&lt;p&gt;From this diagram, I would discuss the underlying problem which would arise in a big project if the said method of directly appending a component to its state.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Props Drilling&lt;/strong&gt;: Which means passing down-state / properties between nested child components, that can be passed down through many child component layers before it finally gets to where it would be used. Even though most child components it passes through do not require it.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;2.&lt;strong&gt;Redundant code&lt;/strong&gt;: this refers to rewriting a bunch of code block in a  repetitive manner.&lt;/p&gt;

&lt;p&gt;Looking at the stated problem above, Redux which actually got inspiration from database design such as event sourcing and happens to be great in the follow.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Great for managing large state.&lt;/li&gt;
&lt;li&gt;Useful for sharing data between components.&lt;/li&gt;
&lt;li&gt;Predictably state management, using the 3 principles below;&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;i). Single source of truth: One big collective object of data&lt;br&gt;
 ii). State appears to be read only: So that it's doesn’t get modified easily&lt;br&gt;
 iii). Changes are made using only pure functions: This simple means a function that returns an output based on the input.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy2z7u4tq0nqj6z1lyqlg.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy2z7u4tq0nqj6z1lyqlg.jpg" alt="with redux"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From the diagram above, we can deduce the difference from diagram 1.0. whereby each component's respective state has been removed and stored in one massive data object. Which in return describes what our application state should look like. In simple terms means &lt;em&gt;no component holds any state&lt;/em&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Redux Operation&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Components that compose our application fires actions, these fired actions set a single piece of the state which can be accessible to all components in the application. The state is what we call a &lt;strong&gt;reducer&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Every reducer is simply a function that takes two arguments a state and an action. Whereby the state can be referred to as the previous state it was in and would end up returning to a new state at the end of the function. Hence, it circles back to the principle listed above which states that it is immutable.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fywsrab0yxo0e4ujf8klt.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fywsrab0yxo0e4ujf8klt.jpg" alt="flux-pattern"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above diagram describes the operational flow of events.&lt;/p&gt;

&lt;p&gt;This pattern is called the &lt;strong&gt;Flux pattern&lt;/strong&gt;: in simple terms an action that gets dispatched in other to update the view. &lt;/p&gt;

&lt;p&gt;This is quite unlike the &lt;strong&gt;MVC Pattern&lt;/strong&gt; (model, view, controller): whereby action is read by the controller, then the model - data is updated based on the controller input. of course, the model updates the view.&lt;/p&gt;

&lt;p&gt;From the &lt;strong&gt;Action:&lt;/strong&gt; let's say an event from a user (such as a click on the mouse or keypress). this action goes through the&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Middleware:&lt;/strong&gt; the middleware can be described as a piece of code that receives the action fired before relaying it to the reducer. &lt;/p&gt;

&lt;p&gt;Then, the &lt;strong&gt;Reducer&lt;/strong&gt; which happens to be a pure function takes an input (state &amp;amp; action) and creates a corresponding state output that can be referred to as the &lt;strong&gt;store&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;And finally, the store (that is the corresponding state) &lt;strong&gt;updates the DOM - view&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Seems to be a lot of states here. So let's clarify with an example.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffd6pexpe72fwey79y9x4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffd6pexpe72fwey79y9x4.png" alt="Example of a reducer"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above is an example of a reducer with a practical approach of that of a user, which consist of two arguments as discussed above.&lt;/p&gt;

&lt;p&gt;This works with a switch case which checks if type is the same as &lt;code&gt;SET_ACTION_TYPE&lt;/code&gt; then we would return a new object via spreading the state. and the main reason we return a new object is to make sure our component re-renders. And if there were no changes made, our component would return the current state by default.&lt;/p&gt;




&lt;p&gt;Thanks for joining once again. Much advance implementation coming up!!! &lt;/p&gt;

</description>
      <category>react</category>
      <category>redux</category>
      <category>state</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
