<?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: Samantha Diaz</title>
    <description>The latest articles on DEV Community by Samantha Diaz (@ssdiaz).</description>
    <link>https://dev.to/ssdiaz</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%2F873393%2F2364e055-2c80-48c2-81c9-1d10c81adc5e.png</url>
      <title>DEV Community: Samantha Diaz</title>
      <link>https://dev.to/ssdiaz</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ssdiaz"/>
    <language>en</language>
    <item>
      <title>Singly vs. Doubly Linked Lists</title>
      <dc:creator>Samantha Diaz</dc:creator>
      <pubDate>Thu, 28 Jul 2022 15:28:34 +0000</pubDate>
      <link>https://dev.to/ssdiaz/singly-vs-doubly-linked-lists-1h5o</link>
      <guid>https://dev.to/ssdiaz/singly-vs-doubly-linked-lists-1h5o</guid>
      <description>&lt;p&gt;This blog briefly compares Singly Linked Lists and Doubly Linked Lists in JavaScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Are Lists?
&lt;/h2&gt;

&lt;p&gt;Lists are a data structure used to keep order of an object. Unlike an &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array"&gt;array&lt;/a&gt;, a list is not an actual data type in JavaScript and does not give its items an index. Instead, lists are created from &lt;a href="https://dev.to/ssdiaz/javascripts-object-overview-6p3"&gt;Objects&lt;/a&gt; and use nodes to point to the previous or next node to keep order, depending on the list type.&lt;/p&gt;

&lt;h2&gt;
  
  
  Singly Linked Lists
&lt;/h2&gt;

&lt;p&gt;To create a singly linked list, first create a class to represent a node, or the items you are collecting in the list. This class represents the blueprint for each item to be added to the list. &lt;/p&gt;

&lt;p&gt;In the below example, I am using &lt;code&gt;Color&lt;/code&gt; to act as the node class. Each color will have a name and a &lt;code&gt;.next&lt;/code&gt; property to reference the next color in the list. The &lt;code&gt;.next&lt;/code&gt; property will default to null when the color is created.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Color {
    constructor(name){
        this.name = name;
        this.next = null;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While we could use this &lt;code&gt;Color&lt;/code&gt; class to create new colors, we'll let our list take care of that in the next example. &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;Rainbow&lt;/code&gt; class is used to maintain our ordered list. The &lt;code&gt;push&lt;/code&gt; method is defined inside the class and is used to create and add items at the end of the list.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Rainbow {
    constructor(){
        this.head = null;
        this.tail = null;
        this.length = 0;
    }
    push(name){
        let newColor = new Color(name);
        if(this.length === 0) {
            this.head = newColor;
            this.tail = this.head;
        } else {
            this.tail.next = newColor;
            this.tail = newColor;
        }
        this.length++;
        return this;
    }

let list = new Rainbow()
list.push("Red")
list.push("Orange")
list.push("Yellow")

console.log(list)
// Rainbow {
//     head: Color { name: 'Red', next: Color { name: 'Orange', next: [Color] } },
//     tail: Color { name: 'Yellow', next: null },
//     length: 3
//   }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that Singly Linked Lists only flow one way - from the head (Red) to the tail (Yellow).&lt;/p&gt;

&lt;p&gt;This can become an issue if you care about time when trying to access the last item in a Singly Linked List, as you must iterate through each item to find the &lt;code&gt;.next&lt;/code&gt; item until you reach the last value. For small lists this may not be a problem, but the larger the list grows, the more time it takes to iterate to the last item, giving a Big O of linear time (O(n)).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Rainbow {

    ...

    get(index){ 
        if(index &amp;lt; 0 || index &amp;gt;= this.length) return null;
        let counter = 0;
        let color = this.head;
        while (counter !== index) {
            color = color.next;
            counter++;
        }
        return color;
    }
}

list.get(1) // this is index of 1, so the 2nd item, Orange
// Color { name: 'Orange', next: Color { name: 'Yellow', next: null } }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Doubly Linked Lists
&lt;/h2&gt;

&lt;p&gt;Doubly Linked Lists are almost identical to a Singly Linked Lists, however they have an additional property called &lt;code&gt;prev&lt;/code&gt; that represents the previous item in the list. This gives us access to move either forward or backwards through the list.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Color {
    constructor(name){
        this.name = name;
        this.next = null;
        this.prev = null;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Rainbow {
    constructor(){
        this.head = null;
        this.tail = null;
        this.length = 0;
    }
    push(name){
        let newColor = new Color(name);
        if(this.length === 0) {
            this.head = newColor;
            this.tail= newColor;
        } else {
            this.tail.next = newColor;
            newColor.prev = this.tail;
            this.tail = newColor;
        }
        this.length++;
        return this;
    }
    get(index){
        if(index &amp;lt; 0 || index &amp;gt;= this.length) return null;
        let count, color;
        if (index &amp;lt;= this.length/2){
            count = 0
            color = this.head;
            while (count != index){
                color = color.next;
                count++;
            }
            console.log("Moved Forward")
        } else {
            count = this.length - 1;
            color = this.tail;
            while (count != index){
                color = color.prev;
                count--;
            }
            console.log("Moved Backward")
        }
        return color;
    }
}

let list = new Rainbow()
list.push("Red")
list.push("Orange")
list.push("Yellow")

console.log(list)
// Rainbow {
//     head: &amp;lt;ref *1&amp;gt; Color {
//       name: 'Red',
//       next: Color { name: 'Orange', next: [Color], prev: [Circular *1] },
//       prev: null
//     },
//     tail: &amp;lt;ref *2&amp;gt; Color {
//       name: 'Yellow',
//       next: null,
//       prev: Color { name: 'Orange', next: [Circular *2], prev: [Color] }
//     },
//     length: 3
//   }

list.get(0)
// "Moved Forward"
// &amp;lt;ref *2&amp;gt; Color {
//   name: 'Red',
//   next: &amp;lt;ref *1&amp;gt; Color {
//     name: 'Orange',
//     next: Color { name: 'Yellow', next: null, prev: [Circular *1] },
//     prev: [Circular *2]
//   },
//   prev: null
// }

list.get(2)
// "Moved Backward"
// &amp;lt;ref *1&amp;gt; Color {
//   name: 'Yellow',
//   next: null,
//   prev: &amp;lt;ref *2&amp;gt; Color {
//     name: 'Orange',
//     next: [Circular *1],
//     prev: Color { name: 'Red', next: [Circular *2], prev: null }
//   }
// }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Difference Between Singly &amp;amp; Doubly Linked Lists
&lt;/h2&gt;

&lt;p&gt;In short, the main difference is that Singly Linked Lists can only move in one direction, which can be unfavorable if you need to access items in the end of the list. Doubly Linked Lists track an extra property called &lt;code&gt;.prev&lt;/code&gt; that allows us to travel in reverse. While time is shorten when accessing with Doubly Linked List, it requires us to store an additional variable in memory.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>JavaScript's Object Overview</title>
      <dc:creator>Samantha Diaz</dc:creator>
      <pubDate>Thu, 28 Jul 2022 00:52:49 +0000</pubDate>
      <link>https://dev.to/ssdiaz/javascripts-object-overview-6p3</link>
      <guid>https://dev.to/ssdiaz/javascripts-object-overview-6p3</guid>
      <description>&lt;p&gt;This is an overview on how to create a basic object class in JavaScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  JS Objects
&lt;/h2&gt;

&lt;p&gt;JavaScript is not an object-oriented programming language. However, you can create 'objects' in JavaScript that behave similar to other OO languages. &lt;/p&gt;

&lt;p&gt;Per the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object"&gt;MDN Document&lt;/a&gt;, an object is a collection of properties.&lt;/p&gt;

&lt;p&gt;JavaScript uses objects as a data type that provides a framework for how a particular item should behave. An instance method is used to define a specific instance's characteristics.&lt;/p&gt;

&lt;h2&gt;
  
  
  Syntax
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    class Dog {
        constructor(name, breed, age, owner){
            this.name = name;
            this.breed = breed;
            this.age = age;
            this.owner = owner;
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  The Constructor Method
&lt;/h3&gt;

&lt;p&gt;Every class uses the constructor method to instantiate a new instance of the object. In my example above, we pass in a &lt;code&gt;name&lt;/code&gt;, &lt;code&gt;breed&lt;/code&gt;, &lt;code&gt;age&lt;/code&gt;, and &lt;code&gt;owner&lt;/code&gt; when we create a new &lt;code&gt;Dog&lt;/code&gt; object. We set &lt;code&gt;this&lt;/code&gt; instance of &lt;code&gt;Dog&lt;/code&gt; to be equal to the variables passed in. The keyword &lt;code&gt;this&lt;/code&gt; refers to that particular instance of &lt;code&gt;Dog&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Note that the above is just the method for creation; no instance of &lt;code&gt;Dog&lt;/code&gt; has yet been created.&lt;/p&gt;

&lt;p&gt;To better understand the object's properties versus the arguments given, you could alternatively set up your constructor per the below. Note that &lt;code&gt;dogName&lt;/code&gt;, &lt;code&gt;dogBreed&lt;/code&gt;, ... are the arguments passed in, and &lt;code&gt;.name&lt;/code&gt;, &lt;code&gt;breed&lt;/code&gt; are the properties each &lt;code&gt;Dog&lt;/code&gt; instance will obtain.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    class Dog {
        constructor(dogName, dogBreed, dogAge, dogOwner){
            this.name = dogName;
            this.breed = dogBreed;
            this.age = dogAge;
            this.owner = dogOwner;
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Create an Object
&lt;/h3&gt;

&lt;p&gt;Create a new object with the &lt;code&gt;new&lt;/code&gt; keyword, followed by the class name. I prefer to store a new object in a variable so we can use it later to modify.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dog1 = new Dog("Fluffy", "Golden Retriever", "4", "Jane Smith") 
// Dog {name: 'Fluffy', breed: 'Golden Retriever', age: '4', owner: 'Jane Smith'}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Retrieve an Object's Data
&lt;/h3&gt;

&lt;p&gt;With our variable, &lt;code&gt;dog1&lt;/code&gt;, we can access that variable's data by calling their properties.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dog1.name
// 'Fluffy'

dog1.breed
// 'Golden Retriever'

dog1.age
// '4'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Give an Object Behavior with Instance Methods
&lt;/h3&gt;

&lt;p&gt;An instance method takes action on that particular &lt;code&gt;Dog&lt;/code&gt; instance it is called on.&lt;/p&gt;

&lt;p&gt;In the example below, the method named &lt;code&gt;bark&lt;/code&gt; gives each instance of &lt;code&gt;Dog&lt;/code&gt; the ability to "woof" when called. Similarly, the &lt;code&gt;ageUp&lt;/code&gt; method increases that specific dog's age by 1 and returns its new age.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    class Dog {
        constructor(name, breed, age, owner){
          ...
        }

       bark(){
           return `${this.name} the dog says woof!`;
       }
       ageUp(){
           this.age += 1;
           return this.age;
       }
    }

dog1.bark()
// "Fluffy the dog says woof!"

dog1.ageUp()
// 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Solve a Frequency Counter Problem in JavaScript with an Empty Object</title>
      <dc:creator>Samantha Diaz</dc:creator>
      <pubDate>Fri, 15 Jul 2022 20:37:53 +0000</pubDate>
      <link>https://dev.to/ssdiaz/solving-a-frequency-counter-problem-in-javascript-try-an-empty-object-d58</link>
      <guid>https://dev.to/ssdiaz/solving-a-frequency-counter-problem-in-javascript-try-an-empty-object-d58</guid>
      <description>&lt;p&gt;Creating an empty object in JavaScript can help to solve technical interview problems that ask you to find a value's frequency in an array.&lt;/p&gt;

&lt;h2&gt;
  
  
  Create an Empty Object
&lt;/h2&gt;

&lt;p&gt;To create an empty object, set a variable equal to hashes. Note that unlike arrays, there is no order in an object and it uses key/value pairings instead of indexes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let myObject = {};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Create Keys from an Array
&lt;/h3&gt;

&lt;p&gt;Given an &lt;code&gt;array&lt;/code&gt;, you can iterate and assign the array's values as the object's keys.&lt;/p&gt;

&lt;h4&gt;
  
  
  Code:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for (let i = 0; i &amp;lt; array.length; i++){ // A
   let key = array[i];                  // B
   if (myObject[key]){                  // C
        myObject[key] += 1;             // D
   } else {                             // E
        myObject[key] = 1;              // F
   }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Pseudocode:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;A) Iterate through the array&lt;/li&gt;
&lt;li&gt;B) Grab the value of each item in the array and set it to variable &lt;code&gt;key&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;C) If &lt;code&gt;key&lt;/code&gt; exists as a key in &lt;code&gt;myObject&lt;/code&gt;... 

&lt;ul&gt;
&lt;li&gt;D) ... increment it by &lt;code&gt;1&lt;/code&gt; &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;E) If not (&lt;code&gt;key&lt;/code&gt; doesn't exist in &lt;code&gt;myObject&lt;/code&gt;)...

&lt;ul&gt;
&lt;li&gt;F) ... create &lt;code&gt;key&lt;/code&gt; in &lt;code&gt;myObject&lt;/code&gt; and set it equal to &lt;code&gt;1&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Refactored Code using &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator"&gt;Conditional (ternary) operator&lt;/a&gt;:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for (let i = 0; i &amp;lt; array.length; i++){
   let key = array[i];
   if myObject[key] ? myObject[key] += 1 : myObject[key] = 1
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  How to Access a Value in an Object
&lt;/h3&gt;

&lt;p&gt;To access a key's value in an object is the same as accessing an index 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;myObject[value];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Solve a Frequency Counter Problem
&lt;/h2&gt;

&lt;p&gt;Suppose you are solving to find the only number in an array that does not repeat. Assume there is only ever 1 answer. &lt;/p&gt;

&lt;p&gt;To solve, you could:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A) Create an empty object&lt;/li&gt;
&lt;li&gt;B) Iterate through the array to capture the array's values &lt;/li&gt;
&lt;li&gt;C) Set the values as keys and increment on each occurrence of the array&lt;/li&gt;
&lt;li&gt;D) Return the key in the hash with a count of 1 to find the non-repeating number
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function findUniqueNumber(array) {  
  let myObject = {};                                          // A

  for (let i = 0; i &amp;lt; array.length; i++){                     // B
      let num = array[i];
      myObject[num] ? myObject[num] +=1 : myObject[num] = 1   // C
  }

  for (let key in myObject){
       if (myObject[key] === 1) return key;                   // D
  }
}

findUniqueNumber( [4, 6, 5, 17, 3, 5, 17, 88, 4, 88, 3] )     // 6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Big O of using an Object
&lt;/h2&gt;

&lt;p&gt;Using brute force gives a Big O of quadratic &lt;code&gt;O(n^2)&lt;/code&gt; because it requires two nested loops. As &lt;code&gt;n&lt;/code&gt; inputs grows, it requires 2 sets of iterations to grab the first number and the next number to  compare and repeats through the array's length. &lt;/p&gt;

&lt;p&gt;By creating an object, we can reduce Big O to linear &lt;code&gt;O(n)&lt;/code&gt;. Here, we iterate twice through the array but on independent loops (&lt;code&gt;2n&lt;/code&gt; can be simplified to &lt;code&gt;n&lt;/code&gt;). So generally speaking, performance is only dependent on the size of the input array.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Swap Array Elements with JavaScript's ES6</title>
      <dc:creator>Samantha Diaz</dc:creator>
      <pubDate>Tue, 12 Jul 2022 15:20:55 +0000</pubDate>
      <link>https://dev.to/ssdiaz/swap-array-elements-with-javascripts-es6-4g12</link>
      <guid>https://dev.to/ssdiaz/swap-array-elements-with-javascripts-es6-4g12</guid>
      <description>&lt;p&gt;If you are solving a problem and it requires you to swap elements in an array, you might create a function like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function swap(array, a, b){
    let temp = array[a];
    array[a] = array[b];
    array[b] = temp;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Code Breakdown
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We define a function called &lt;code&gt;swap&lt;/code&gt;, and pass in arguments for an &lt;code&gt;array&lt;/code&gt;, variable &lt;code&gt;a&lt;/code&gt;, and variable &lt;code&gt;b&lt;/code&gt;. These represent the values we want to swap. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Next, we declare a temporary variable, &lt;code&gt;temp&lt;/code&gt;, and set that equal to the value of &lt;code&gt;a&lt;/code&gt; in the &lt;code&gt;array&lt;/code&gt;. We need to hold this value in a variable to refer to it later. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Then, we set the value of &lt;code&gt;a&lt;/code&gt; in &lt;code&gt;array&lt;/code&gt; to the value of &lt;code&gt;b&lt;/code&gt; in &lt;code&gt;array&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Finally, we set the value of &lt;code&gt;b&lt;/code&gt; in &lt;code&gt;array&lt;/code&gt; to the value of our &lt;code&gt;temp&lt;/code&gt; variable. Notice that we can't set it to the value of &lt;code&gt;a&lt;/code&gt; in &lt;code&gt;array&lt;/code&gt; as we have already changed that to equal &lt;code&gt;b&lt;/code&gt;'s value. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  BUT - with ES6, there's a simpler way!
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const swap = (array, a, b) =&amp;gt; {
    [array[a], array[b]] = [array[b], array[a]];
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This uses &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment"&gt;Destructuring Assignment&lt;/a&gt; to separate the elements of the array into values that can be reassigned. &lt;/p&gt;

&lt;h4&gt;
  
  
  Code Breakdown
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;To make the code more compact, we declare the function using an &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions"&gt;arrow function&lt;/a&gt; named &lt;code&gt;swap&lt;/code&gt; from ES6.&lt;/li&gt;
&lt;li&gt;This function takes the same arguments: &lt;code&gt;array&lt;/code&gt;, &lt;code&gt;a&lt;/code&gt;, &lt;code&gt;b&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;Instead of requiring the &lt;code&gt;temp&lt;/code&gt; variable, we can simply assign the &lt;code&gt;array&lt;/code&gt;'s value of &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; to the respective opposite value on the right side of the equal sign.&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>Difference Between 'for...in' and 'for...of' Loop s</title>
      <dc:creator>Samantha Diaz</dc:creator>
      <pubDate>Fri, 08 Jul 2022 17:27:51 +0000</pubDate>
      <link>https://dev.to/ssdiaz/difference-between-forin-and-forof-loop-s-158g</link>
      <guid>https://dev.to/ssdiaz/difference-between-forin-and-forof-loop-s-158g</guid>
      <description>&lt;p&gt;This blog summarizes when to use 'for...in' and 'for...of' loops in JavaScript.&lt;/p&gt;

&lt;h3&gt;
  
  
  Difference
&lt;/h3&gt;

&lt;p&gt;The '&lt;strong&gt;for...in&lt;/strong&gt;' loop is used for &lt;strong&gt;objects&lt;/strong&gt;. The '&lt;strong&gt;for...of&lt;/strong&gt;' loop is used on data structures like &lt;strong&gt;arrays&lt;/strong&gt; and &lt;strong&gt;strings&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It is possible to use a &lt;strong&gt;for in&lt;/strong&gt; loop with arrays and strings. However, per JavaScript's &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of#difference_between_for...of_and_for...in"&gt;MDN&lt;/a&gt;, the &lt;strong&gt;for of&lt;/strong&gt; loop is preferred for strings and arrays as it returns the index as a number, whereas the &lt;strong&gt;for in&lt;/strong&gt; loop will return the index as a string.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to remember?
&lt;/h3&gt;

&lt;p&gt;Think about &lt;strong&gt;for in&lt;/strong&gt; loops as being &lt;strong&gt;inside&lt;/strong&gt; an object. Since objects have no order, every item is associated &lt;em&gt;inside&lt;/em&gt; it, all together. It's like a purse containing all your items - no order, everything is just inside. &lt;/p&gt;

&lt;p&gt;Since arrays and strings have order, think about &lt;strong&gt;for of&lt;/strong&gt; loops as in every item &lt;strong&gt;of&lt;/strong&gt; an &lt;strong&gt;array&lt;/strong&gt; or &lt;strong&gt;string&lt;/strong&gt;. If you were reading a book (array) with chapters (indexes), you would say you're currently "reading &lt;u&gt;chapter 3 &lt;strong&gt;of&lt;/strong&gt; 30&lt;/u&gt; in the book". So, think of it as  'for X index &lt;strong&gt;OF&lt;/strong&gt; an array'.&lt;/p&gt;

&lt;h1&gt;
  
  
  For...in
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;For...in&lt;/strong&gt; loops iterate through an &lt;strong&gt;object&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;From the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in"&gt;MDN Web Doc&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for (const variable in object) {
  statement
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Examples
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const object = { a: 1, b: 2, c: 3 };

for (const property in object) {
  console.log(`${property}: ${object[property]}`);
}

// "a: 1"
// "b: 2"
// "c: 3"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const obj = { a: 1, b: 2, c: 3 };
for (var i in obj) {
  console.log(i);
}

// "a"
// "b"
// "c"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  For...of
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;For...of&lt;/strong&gt; loops iterate over an &lt;strong&gt;array&lt;/strong&gt; or &lt;strong&gt;string&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;From the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of"&gt;MDN Web Doc&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for (variable of iterable) {
  statement
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Examples
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array1 = ['a', 'b', 'c'];

for (const element of array1) {
  console.log(element);
}

// "a"
// "b"
// "c"

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

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const string = 'boo';

for (const value of string) {
  console.log(value);
}
// "b"
// "o"
// "o"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Algorithms - Two Pointers Pattern</title>
      <dc:creator>Samantha Diaz</dc:creator>
      <pubDate>Mon, 27 Jun 2022 22:41:50 +0000</pubDate>
      <link>https://dev.to/ssdiaz/algorithms-two-pointers-pattern-46ai</link>
      <guid>https://dev.to/ssdiaz/algorithms-two-pointers-pattern-46ai</guid>
      <description>&lt;p&gt;This covers the Two Pointer Pattern algorithm for technical interviews in JavaScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  When to Use the Two Pointer Pattern
&lt;/h2&gt;

&lt;p&gt;The Two Pointer Pattern is helpful when you need to perform a search to find a pair of numbers that meet a certain condition within a sorted data set. &lt;/p&gt;

&lt;p&gt;For example, you can use this method to solve to find the first two numbers in an array that sums to a desired value.&lt;/p&gt;

&lt;p&gt;The Two Pointer Pattern only works if your data set is &lt;strong&gt;sorted&lt;/strong&gt;. Be sure to ask your interviewer if the data set is already sorted or if it can be.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Use the Two Pointer Pattern
&lt;/h2&gt;

&lt;p&gt;Let's assume you are solving to find the first set of numbers that sum to zero in a sorted array. &lt;/p&gt;

&lt;p&gt;Instead of using brute force and nested loops to check the 1st item with the 2nd, then the 1st item with the 3rd and so on, you can set up pointers in the first (left) and last (right) position of the array to compare.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UApwSuMG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a6n74amn8xwedyol6wz1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UApwSuMG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a6n74amn8xwedyol6wz1.png" alt="steps to solve for sum of 3" width="724" height="188"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Move the pointers based on the sum of the values:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If the sum of the pointers are greater than the solve for 0, it means the highest number is too large, so move the last pointer (right) down 1 position. &lt;/li&gt;
&lt;li&gt;If the sum of the pointers are less than the solve for 0, it means your smallest number is too small, so move the first pointer (left) up 1 position. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Repeat this logic until the pointers meet or you solve for the desired sum.&lt;/p&gt;

&lt;h3&gt;
  
  
  Hasn't clicked yet?
&lt;/h3&gt;

&lt;p&gt;If the biggest number (right pointer) is paired with the smallest number (left pointer) and their sum is &lt;em&gt;greater than&lt;/em&gt; the desired value, then no other number paired with the right pointer will result in the desired sum as it will continue to be too high. So, move the right pointer one position over towards the center, keep the left pointer the same, and run through the iteration again.&lt;/p&gt;

&lt;p&gt;The same idea applies if the sum of the pointers is too small - you move the left pointer over one spot towards the center. If the sum of smallest number (left pointer) and largest number (right pointer) is &lt;em&gt;less than&lt;/em&gt; the sum, then there is no number remaining in the array that can be paired with the smallest number to meet your desired sum - since even paired with the highest number is too low.&lt;/p&gt;

&lt;h2&gt;
  
  
  Visual Examples
&lt;/h2&gt;

&lt;p&gt;In this example, we solve for the first pair that sums to 0. Notice that the data set is sorted from smallest to largest. &lt;/p&gt;

&lt;p&gt;If you take the first number at index 0 (-3) and the last number at index 6 (3), you'll find the answer in one iteration - the sum equals zero!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zdy8ibma--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7k3kh5krmgpw3j8go9i8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zdy8ibma--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7k3kh5krmgpw3j8go9i8.png" alt="steps to solve for sum of 0" width="880" height="121"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;What if you are solving for the first pair that sums to 2? This completes in 3 iterations.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KQNsmd2H--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/reg3uc3yuypzoqi2d7st.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KQNsmd2H--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/reg3uc3yuypzoqi2d7st.png" alt="steps to solve for sum of 2" width="880" height="216"&gt;&lt;/a&gt;&lt;/p&gt;



&lt;p&gt;Here's another example, solving for sum of 10. Completes in 5 iterations:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--H_I49-ms--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3ro6vy74xdpzrjp6aaz3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--H_I49-ms--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3ro6vy74xdpzrjp6aaz3.png" alt="steps to solve for sum of 10" width="880" height="364"&gt;&lt;/a&gt;&lt;/p&gt;



&lt;p&gt;Last example, solving for sum of 30. Here, the solution is not found and it uses the max amount of iterations, 4:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--srdm14r---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iduy5djpcf6slb90p0br.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--srdm14r---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iduy5djpcf6slb90p0br.png" alt="steps to solve for sum of 30" width="880" height="342"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  How to Code Two Pointer Solution
&lt;/h2&gt;

&lt;p&gt;Example: return an array of the first pair that sums to zero.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function zeroSum(arr) {
   let left = 0;
   let right = arr.length - 0;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Explain the Code Line by Line
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function zeroSum(arr) {
  ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Declare the JavaScript function, passing in &lt;code&gt;arr&lt;/code&gt; as a parameter for a data set.&lt;/li&gt;
&lt;/ul&gt;






&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   let left = 0;
   let right = arr.length - 0;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Here we declare our pointer's initial index. &lt;/li&gt;
&lt;li&gt;The &lt;code&gt;left&lt;/code&gt; pointer will always start at 0, since an array's index starts at 0. &lt;/li&gt;
&lt;li&gt;The &lt;code&gt;right&lt;/code&gt; pointer starts at the last index in our array, which we find by getting the array length and subtracting by 1 to get it's index.&lt;/li&gt;
&lt;/ul&gt;






&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   while(left &amp;lt; right){
     ...
   }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;We set our loop to run only while our &lt;code&gt;left&lt;/code&gt; pointer is less than our &lt;code&gt;right&lt;/code&gt; pointer. Otherwise we'd duplicate iterations if our &lt;code&gt;left&lt;/code&gt; pointer is greater than or equal to the &lt;code&gt;right&lt;/code&gt; pointer.&lt;/li&gt;
&lt;/ul&gt;






&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;      let sum = arr[left] + arr[right];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;We declare &lt;code&gt;sum&lt;/code&gt; inside the &lt;code&gt;while&lt;/code&gt; loop so that the respective pointers update on each iteration.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;sum&lt;/code&gt; equals the value at the &lt;code&gt;left&lt;/code&gt; pointer's index plus the value at the &lt;code&gt;right&lt;/code&gt; pointer's index.&lt;/li&gt;
&lt;/ul&gt;






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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;if&lt;/code&gt; statement sets the pointers based on our condition. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;First, if the &lt;code&gt;sum&lt;/code&gt; equals 0, then return the values in an array itself. This &lt;code&gt;return&lt;/code&gt; will exit the &lt;code&gt;while&lt;/code&gt; loop on the first iteration that matches our criteria. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If &lt;code&gt;sum&lt;/code&gt; is greater than 0, then the numbers are too large. Decrease the &lt;code&gt;right&lt;/code&gt; pointer one space to the left.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Else (if &lt;code&gt;sum&lt;/code&gt; is less than 0), then the numbers are too small. Increase the &lt;code&gt;left&lt;/code&gt; pointer one space to the right.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Big O of the Two Pointer Pattern
&lt;/h2&gt;

&lt;p&gt;Using brute force with nested loops gives a time complexity of O(n^2) as you have to loop through each item in the array to grab the 1st number, then loop again through each item to check it against the 2nd. &lt;/p&gt;

&lt;p&gt;Using the Two Pointer Pattern reduces the time complexity to be more favorable at O(n). This pattern will at most only iterate the length of the array minus 1, since we compare in pairs.&lt;/p&gt;

&lt;p&gt;Space to solve this problem will be constant at O(1).&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Before You Begin The Technical Interview ...</title>
      <dc:creator>Samantha Diaz</dc:creator>
      <pubDate>Thu, 23 Jun 2022 15:53:09 +0000</pubDate>
      <link>https://dev.to/ssdiaz/before-you-begin-the-technical-interview--123o</link>
      <guid>https://dev.to/ssdiaz/before-you-begin-the-technical-interview--123o</guid>
      <description>&lt;p&gt;These are tips on how to begin the technical interview before you start to code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understand the Problem
&lt;/h2&gt;

&lt;p&gt;It seems obvious - but make sure you understand the problem you are asked to solve for. Ask your interviewer any questions to clarify the problem. &lt;/p&gt;

&lt;p&gt;I find it helpful to summarize the problem back to the interviewer to make sure I can explain it in my own words. This helps to confirm that my interpretation of the problem is correct. Nothing's worse than wasting time solving a problem that wasn't even asked!&lt;/p&gt;

&lt;h2&gt;
  
  
  Ask About Inputs
&lt;/h2&gt;

&lt;p&gt;Ask what assumptions can be made about the inputs or arguments. If the problem says it accepts an array, will that array be sorted or unsorted? If it accepts a string, will it vary in length and characters? Will the user always provide 2 arguments, what if they only give 1?&lt;/p&gt;

&lt;h2&gt;
  
  
  Ask About Edge Cases
&lt;/h2&gt;

&lt;p&gt;Edge cases are the most extreme inputs that can be used in the problem. If the problem accepts a string as data type, what happens if the user inputs a number, or array? What if the problem accepts an array, but the user provides an empty array? These are questions to ask the interviewer to further clarify the problem, but to also give you more understanding on what you need to return.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Should Be Returned From Your Solution?
&lt;/h2&gt;

&lt;p&gt;Your solution needs to return something - make sure you fully understand what. Should it return a value, or index of a data set? What data type should be returned, a string or integer? Is the problem asking to return the count of duplicates or does it want the pair values themselves?&lt;/p&gt;

&lt;p&gt;Here, you can also think about what to return based on particular inputs. If the input is empty, or null, what should your solution return?&lt;/p&gt;

&lt;h2&gt;
  
  
  Time to code, but first... Write Pseudocode!
&lt;/h2&gt;

&lt;p&gt;Writing pseudocode is helpful because it allows you to quickly organize your thoughts before spending time writing perfect, working code. If you draft your solution in comments, this gives you time to think about the Big O of your solution incase you want to try a different approach. It also gives you space to see if there's anything that can be quickly refactored.&lt;/p&gt;

&lt;p&gt;One thing to note - make sure you tell your interviewer that you are writing pseudocode! While this may seem obvious to you, it may not be to your interviewer and they may think this is what you consider real code to be. Don't leave any room for interpretations and tell the interviewer this is only used to draft your ideas, and that you will live code shortly after.&lt;/p&gt;

&lt;h2&gt;
  
  
  Stuck on Part of the Problem? Code What You Can!
&lt;/h2&gt;

&lt;p&gt;After you've pseudo-coded and drafted your ideas, start where you can. If you can confidently solve for part of the problem, do it! Maybe you only know how to code the return value - that's ok! You have a limited time to solve for the problem, and it's better to show for something than to get stuck on one small area of the solution and not present anything.  &lt;/p&gt;

&lt;h2&gt;
  
  
  Above Anything Else - Think Out-loud!
&lt;/h2&gt;

&lt;p&gt;The interviewer wants to understand your thought process - so narrate it! Sure, you can code in silence and let your solution speak for itself, but being quiet doesn't allow you to show off your extensive knowledge. You're trying to impress them for the job, after-all.&lt;/p&gt;

&lt;p&gt;For example, you know that array indexes start at 0 and that's why you started your loop at i = 0, but make it known and say it out-loud! This also highlights your communication skills to the interviewer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Don't Forget to Refactor!
&lt;/h2&gt;

&lt;p&gt;While this last part typically comes when you're done with the coding challenge, talk with you interviewer on what could be refactored. If you have the time - code it. If not, quickly explain where your mind is headed next and where you'd look to improve your code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lastly - Be Confident and Trust Yourself!
&lt;/h2&gt;

&lt;p&gt;If you've landed a technical interview, you're doing something right. Push out negative thoughts or self-doubt. You've studied, you've practiced, and you're going to do great so long as you keep a positive mind set. &lt;/p&gt;

&lt;p&gt;It's ok if you can't fully solve the problem - just show what you can. The interviewer will appreciate how communicative you are by asking questions and narrating your thought process. They'll think you have strong attention to detail if you ask about edge cases. And they'll see you as a problem solver if you're able to use Google to supplement your solution. These are still valuable traits to display even if you get stuck on the solution. Code what you know and you'll do fine!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Quick Summary on Big O Notation</title>
      <dc:creator>Samantha Diaz</dc:creator>
      <pubDate>Tue, 21 Jun 2022 15:14:21 +0000</pubDate>
      <link>https://dev.to/ssdiaz/quick-summary-on-big-o-notation-1360</link>
      <guid>https://dev.to/ssdiaz/quick-summary-on-big-o-notation-1360</guid>
      <description>&lt;p&gt;This blog gives a short summary on Big O Notation, specifically for technical interviews. &lt;/p&gt;

&lt;h2&gt;
  
  
  What is Big O Notation?
&lt;/h2&gt;

&lt;p&gt;The "O" in Big O Notation (that's "O'" the letter, not a zero) comes from the German word "Ordnung" which means the order of approximation. Big O is used to estimate the performance of code based on its time and space complexity. It's a way to compare performance and runtime in the event inputs were to grow indefinitely - or, to the biggest number you can think of.&lt;/p&gt;

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

&lt;p&gt;The ideal solution will have the smallest, or less steep, Big O as possible. The most common Big O results are: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Constant, O(1)&lt;/strong&gt; - operations, un-ordered data structures like object manipulation, variable assignments, inserting/deleting at the end of an ordered data structure&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Linear, O(n)&lt;/strong&gt; - loops &amp;amp; iterations, anytime you need to search or step through an ordered data structured, inserting/deleting at beginning of an ordered data structure&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Log, O(log n)&lt;/strong&gt; - recursion functions, searching algorithms&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Quadratic, O(n^2)&lt;/strong&gt; - nested loops &amp;amp; iterations&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;To calculate Big O, count the number of operations in your code snippet and count as many times as the code runs. This means that if you are looping (or iterating), the code would perform the operation &lt;em&gt;n&lt;/em&gt; amount of times. If you use a nested loop, this means the operation runs &lt;em&gt;n&lt;/em&gt; * &lt;em&gt;n&lt;/em&gt; number of times - so this would be quadratic O(n^2). If you add a number then multiply it by something, you are performing 2 mathematical operations and hence would be a constant O(2).&lt;/p&gt;

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

&lt;p&gt;Big O should be simplified to its lowest form. For example, if your code has 5 mathematical operations and involves an 2 iterations, this can be simplified to O(5 + 2n) -&amp;gt; O(n). The idea behind this is that a huge number will still be a huge number if multiplied by 2 and added 5.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Trick to Reduce Big O with Arrays
&lt;/h2&gt;

&lt;p&gt;When dealing with manipulating arrays, order matters. If you add (.unshift) or remove (.shift) to the beginning of an array, you are shifting every item in the array and hence every item needs to be re-indexed. This creates a linear Big O, O(n). Instead, add (.push) or remove (.pop) from the end of the array, so that the other indexes in the array remain unchanged. This gives a more ideal Big O: constant O(1).&lt;/p&gt;

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