avatarDarshna Rekha

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2490

Abstract

te through the array to allocate memory to each item. In these scenarios, the complexity will not be <code>O(1)</code> but <code>O(N)</code>.</p><p id="46be">But this is a <b><i>minor</i></b> detail and we might not have to worry about memory allocations or the possibility of copying the array to a new location.</p><p id="da8f">Append — <code>O(1)</code>*</p><p id="054c">*Can be <code>O(N)</code></p><p id="8aa4">We can assume that the push command (for dynamic arrays) is <code>O(1)</code><b>constant</b> in time, <i>but</i> once in a while, we can encounter a situation when it is <b>linear</b> in time.</p><div id="3257"><pre>console.log(things); <span class="hljs-regexp">//</span> [<span class="hljs-string">'Books'</span>, <span class="hljs-string">'Dog'</span>, <span class="hljs-string">'Home'</span>, <span class="hljs-string">'Money'</span>]</pre></div><h1 id="6f64">Array Implementation in JS</h1><p id="bed7">In this section let us implement Array in JavaScript. The implementation is for viewing the complexity.</p><p id="e010">But before we get into the operations I recommend brushing up the most basic operations on Arrays in JavaScript.</p><div id="0065" class="link-block"> <a href="https://darshnarekha09.medium.com/arrays-basic-operations-javascript-558c155489ab"> <div> <div> <h2>Arrays Basic Operations — JavaScript</h2> <div><h3>List of the most basic operations on Arrays in JavaScript. Try implementing them on your own — good for brushing up…</h3></div> <div><p>darshnarekha09.medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*rsr224iuqYzudEmOTjgYqA.png)"></div> </div> </div> </a> </div><p id="a598">We will be using the following boilerplate code to implement arrays. You can <b>try</b> implementing the operations — <code>lookup()</code> or <code>get()</code>, <code>push()</code>, <code>pop()</code>, and <code>delete()</code>.</p> <figure id="092e"> <div> <div>

            <iframe class="gist-iframe" src="/gist/darshna09/2dac497602fe7c5c1d41cabce421002b.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="4e8c">We will be using an <b>obje

Options

ct</b> to implement arrays in JavaScript — after all an array in JavaScript is an Object.</p><blockquote id="adda"><p>The JavaScript <code><b>Array</b></code> class is a global object that is used in the construction of arrays; which are high-level, list-like objects.</p></blockquote><blockquote id="9f0f"><p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array">Array — JavaScript | MDN</a></p></blockquote><p id="42d5">For each element, we will add an object in the format <code>index: value</code>and keep a <i>tab</i> on the <code>length</code>. We can access the item using the <code>index</code> key.</p><p id="cedc">To delete an element we will need to shift the elements after the deleted element one index up.</p> <figure id="6b4f"> <div> <div>

            <iframe class="gist-iframe" src="/gist/darshna09/e191ea82efdde78bb7ce611ff19f6f9e.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><h1 id="9326">Strings and Arrays</h1><p id="a771">Strings are an array of characters. Usually, interview questions on strings can be treated as arrays.</p>
    <figure id="24f8">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/darshna09/68591b2168f302d122e16421ee439e2c.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><blockquote id="91d5"><p>Split the string, perform the operations and then return the string.</p></blockquote><h1 id="a3e3">Conclusion</h1><p id="5ed3">Before choosing arrays for your program keep a watch on the following points.</p><p id="039a">✅ Fast lookups</p><p id="82b7">✅ Fast push/pop</p><p id="5db7">✅ Ordered</p><p id="0fea">❌ Slow insert</p><p id="1f1d">❌ Slow delete</p><p id="4b45">❌ Fixed-size (if using static array)</p><p id="1061">Choosing the right data structure is important and we should know when to use what. I hope this article gives that insight for arrays.</p><h1 id="6530">Further Reading</h1><p id="2704"><a href="https://readmedium.com/data-structure-with-javascript-2d3791598453">Data Structure With JavaScript</a></p><p id="20a7"><a href="https://readmedium.com/ds-with-js-hash-tables-f8fec13ad12a">DS With JS — Hash Tables</a></p></article></body>

DS With JS — Arrays

Arrays — JavaScript (Made with Excalidraw)

Arrays or Lists are organized sequentially. They’re stored in contiguous memory. If you need to store some data and iterate over it, arrays are the best choice, especially if you know the indices.

Static & Dynamic Arrays

Static arrays are fixed in size. We need to specify the number of elements in the array while declaring it.

If we want to declare an array of groceries of 7 items, 7 consecutive addresses will be allocated in memory. If we realize later that we need to add one more item we will have to have dynamic arrays.

Dynamic arrays allow us to copy and rebuild an array at a new location with more memory (if current memory cannot accommodate the new items). If we want to add the 8th item to the grocery list, the entire list of 7 items is copied to a new location, allocate 14 blocks of memory (usually twice the current size), and add the 8th item in the new location.

Dynamic Arrays in JS

In JavaScript the lists or arrays are dynamic. They automatically allocate memory with the increase in the size of arrays.

Dynamic Arrays can have O(N) complexity!

const things = ['Books', 'Car', 'Home', 'Money'];
things.push('Boat');    // 5
things.push('Goat');    // 6

It is going to push 'Boat'and 'Goat' at the end of the array. But if the memory allocated for this array doesn't have space for the 5th item, it will allocate new memory with more space (usually doubles the existing memory), and iterate through the array to allocate memory to each item. In these scenarios, the complexity will not be O(1) but O(N).

But this is a minor detail and we might not have to worry about memory allocations or the possibility of copying the array to a new location.

Append — O(1)*

*Can be O(N)

We can assume that the push command (for dynamic arrays) is O(1)constant in time, but once in a while, we can encounter a situation when it is linear in time.

console.log(things);    // ['Books', 'Dog', 'Home', 'Money']

Array Implementation in JS

In this section let us implement Array in JavaScript. The implementation is for viewing the complexity.

But before we get into the operations I recommend brushing up the most basic operations on Arrays in JavaScript.

We will be using the following boilerplate code to implement arrays. You can try implementing the operations — lookup() or get(), push(), pop(), and delete().

We will be using an object to implement arrays in JavaScript — after all an array in JavaScript is an Object.

The JavaScript Array class is a global object that is used in the construction of arrays; which are high-level, list-like objects.

Array — JavaScript | MDN

For each element, we will add an object in the format index: valueand keep a tab on the length. We can access the item using the index key.

To delete an element we will need to shift the elements after the deleted element one index up.

Strings and Arrays

Strings are an array of characters. Usually, interview questions on strings can be treated as arrays.

Split the string, perform the operations and then return the string.

Conclusion

Before choosing arrays for your program keep a watch on the following points.

✅ Fast lookups

✅ Fast push/pop

✅ Ordered

❌ Slow insert

❌ Slow delete

❌ Fixed-size (if using static array)

Choosing the right data structure is important and we should know when to use what. I hope this article gives that insight for arrays.

Further Reading

Data Structure With JavaScript

DS With JS — Hash Tables

JavaScript
Arrays
Data Structures
Basics
Recommended from ReadMedium