<?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: Gabriel Maciel</title>
    <description>The latest articles on DEV Community by Gabriel Maciel (@berutodo).</description>
    <link>https://dev.to/berutodo</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%2F944298%2Fad27402f-a324-4637-a9e7-6d2f168a1179.png</url>
      <title>DEV Community: Gabriel Maciel</title>
      <link>https://dev.to/berutodo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/berutodo"/>
    <language>en</language>
    <item>
      <title>Binary Search</title>
      <dc:creator>Gabriel Maciel</dc:creator>
      <pubDate>Sat, 25 Feb 2023 03:10:53 +0000</pubDate>
      <link>https://dev.to/berutodo/binary-search-18e9</link>
      <guid>https://dev.to/berutodo/binary-search-18e9</guid>
      <description>&lt;h2&gt;
  
  
  What is Binary Search?
&lt;/h2&gt;

&lt;p&gt;A binary search is an algorithm used to find the position of a specific value in a sorted array. The algorithm repeatedly divides the search interval in half until the value is found or the search interval becomes empty.&lt;/p&gt;

&lt;p&gt;Example of a Binary Search function in Javascript:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;binarySearch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;The binarySearch function takes two arguments: the array to be searched (arr) and the target value to be found (target). It initializes two pointers, left and right, to the beginning and end of the array, respectively.&lt;/p&gt;

&lt;p&gt;The function then enters a while loop that continues as long as left is less than or equal to right. In each iteration, the function calculates the midpoint index (mid) of the search interval using the formula (left + right) / 2. It then compares the value at the midpoint with the target value.&lt;/p&gt;

&lt;p&gt;If the midpoint value is equal to the target value, the function returns the midpoint index. If the midpoint value is less than the target value, the function updates left to mid + 1, effectively discarding the left half of the search interval. If the midpoint value is greater than the target value, the function updates right to mid - 1, effectively discarding the right half of the search interval.&lt;/p&gt;

&lt;p&gt;If the function completes the while loop without finding the target value, it returns -1 to indicate that the value was not found in the array.&lt;/p&gt;

&lt;p&gt;Here's an example of how to use the binarySearch function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;binarySearch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: 6&lt;/span&gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  When to use Binary Search?
&lt;/h2&gt;

&lt;p&gt;Binary search is a very efficient algorithm for searching in a sorted array or list. It has a time complexity of O(log n), which is much faster than linear search (which has a time complexity of O(n)).&lt;/p&gt;

&lt;p&gt;Binary search is particularly useful when searching through very large datasets or when search performance is critical. It is commonly used in applications such as search engines, databases, and scientific computing.&lt;/p&gt;

&lt;p&gt;Binary search is most effective when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The data is sorted: Binary search requires the data to be sorted in order for the algorithm to work correctly. If the data is not sorted, the algorithm may not find the correct answer.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The data is static or nearly static: If the data is constantly changing, binary search may not be the best algorithm to use. Each time the data changes, the sorting process must be repeated, which can be very time-consuming.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The data size is large: Binary search is most effective when the data set is large. If the data set is small, the overhead of sorting the data and implementing the algorithm may not be worth the performance benefits.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In summary, binary search is a powerful algorithm that can provide significant performance improvements in the right situations. If you need to search through a large dataset that is static or nearly static and sorted, binary search is a great option to consider.&lt;/p&gt;

</description>
      <category>python</category>
      <category>devops</category>
      <category>backend</category>
      <category>ai</category>
    </item>
    <item>
      <title>Top 10 Array Methods in JavaScript</title>
      <dc:creator>Gabriel Maciel</dc:creator>
      <pubDate>Wed, 22 Feb 2023 19:13:04 +0000</pubDate>
      <link>https://dev.to/berutodo/top-10-array-methods-in-javascript-4b4f</link>
      <guid>https://dev.to/berutodo/top-10-array-methods-in-javascript-4b4f</guid>
      <description>&lt;p&gt;JavaScript arrays are one of the most commonly used data structures in the language. Arrays are a powerful and flexible tool that can be used to store and manipulate data in a variety of ways. Here are the top 10 array methods in JavaScript along with examples of how they can be used:&lt;/p&gt;

&lt;h2&gt;
  
  
  1. push()
&lt;/h2&gt;

&lt;p&gt;The push() method adds one or more elements to the end of an array and returns the new length of the array.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array1 = [1, 2, 3];
array1.push(4);
console.log(array1); // [1, 2, 3, 4]

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. pop()
&lt;/h2&gt;

&lt;p&gt;The pop() method removes the last element from an array and returns that element.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array1 = [1, 2, 3];
const lastElement = array1.pop();
console.log(lastElement); // 3
console.log(array1); // [1, 2]

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. shift()
&lt;/h2&gt;

&lt;p&gt;The shift() method removes the first element from an array and returns that element.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array1 = [1, 2, 3];
const firstElement = array1.shift();
console.log(firstElement); // 1
console.log(array1); // [2, 3]

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  4. unshift()
&lt;/h2&gt;

&lt;p&gt;The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array1 = [1, 2, 3];
array1.unshift(4, 5);
console.log(array1); // [4, 5, 1, 2, 3]

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  5. slice()
&lt;/h2&gt;

&lt;p&gt;The slice() method returns a shallow copy of a portion of an array into a new array object.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array1 = [1, 2, 3, 4, 5];
const array2 = array1.slice(1, 3);
console.log(array2); // [2, 3]

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  6. splice()
&lt;/h2&gt;

&lt;p&gt;The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array1 = [1, 2, 3, 4, 5];
array1.splice(2, 1, 6);
console.log(array1); // [1, 2, 6, 4, 5]

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  7. concat()
&lt;/h2&gt;

&lt;p&gt;The concat() method is used to merge two or more arrays into a new array.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array1 = [1, 2, 3];
const array2 = [4, 5, 6];
const newArray = array1.concat(array2);
console.log(newArray); // [1, 2, 3, 4, 5, 6]

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  8. filter()
&lt;/h2&gt;

&lt;p&gt;The filter() method creates a new array with all elements that pass the test implemented by the provided function.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array1 = [1, 2, 3, 4, 5];
const filteredArray = array1.filter(num =&amp;gt; num &amp;gt; 2);
console.log(filteredArray); // [3, 4, 5]

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  9. map()
&lt;/h2&gt;

&lt;p&gt;The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array1 = [1, 2, 3];
const mappedArray = array1.map(num =&amp;gt; num * 2);
console.log(mappedArray); // [2, 4, 6]

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  10. reduce()
&lt;/h2&gt;

&lt;p&gt;The reduce() method applies a function against an accumulator and each element in the array to reduce it to a single value.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array1 = [1, 2, 3, 4, 5];
const reducedValue = array1.reduce((accumulator, currentValue) =&amp;gt; accumulator + currentValue);
console.log(reducedValue); // 15

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

&lt;/div&gt;



&lt;p&gt;These are the top 10 array methods in JavaScript. There are many other methods available that can help you manipulate arrays in different ways. Understanding these methods will help you write more efficient and concise code.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
