<?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: Jojo Zhang</title>
    <description>The latest articles on DEV Community by Jojo Zhang (@nomadkitty).</description>
    <link>https://dev.to/nomadkitty</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%2F208648%2F325ff072-fd4d-4749-b112-a4e3740c1f32.jpg</url>
      <title>DEV Community: Jojo Zhang</title>
      <link>https://dev.to/nomadkitty</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nomadkitty"/>
    <language>en</language>
    <item>
      <title>How to Solve Sock Merchant Code Challenge</title>
      <dc:creator>Jojo Zhang</dc:creator>
      <pubDate>Thu, 02 Jul 2020 21:48:17 +0000</pubDate>
      <link>https://dev.to/nomadkitty/how-to-solve-sock-merchant-code-challenge-5034</link>
      <guid>https://dev.to/nomadkitty/how-to-solve-sock-merchant-code-challenge-5034</guid>
      <description>&lt;p&gt;I started the Interview Preparation Kit on Hacker Rank yesterday.  The thought of why not sharing how I solve those problems came to my mind.  And here I am!&lt;/p&gt;

&lt;p&gt;In this article, I'll use the UPER (&lt;strong&gt;Understand&lt;/strong&gt;, &lt;strong&gt;Plan&lt;/strong&gt;, &lt;strong&gt;Execute&lt;/strong&gt;, &lt;strong&gt;Reflect&lt;/strong&gt;) way of the problem-solving framework that I learned in Lambda School Computer Science Curriculum to walk through problems.&lt;/p&gt;

&lt;h1&gt;
  
  
  The challenge
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://www.hackerrank.com/challenges/sock-merchant/problem" rel="noopener noreferrer"&gt;HackerRank Sock Merchant Challenge Page&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;John works at a clothing store. He has a large pile of socks that he must pair by color for sale. Given an array of integers representing the color of each sock, determine how many pairs of socks with matching colors there are.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1: Understand
&lt;/h3&gt;

&lt;p&gt;In this step, I'd dissect the problem into following questions,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;What is the goal?&lt;/strong&gt;&lt;br&gt;
To count the number of how many pairs of socks.  This also gives info that the final result should be an integer. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;What do I have for parameters?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;n: the number(&lt;em&gt;interger&lt;/em&gt;) of socks in the pile&lt;/li&gt;
&lt;li&gt;ar: an array of the colors (&lt;em&gt;also integer to represent the value of each color&lt;/em&gt;) of each sock&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;How to achieve the goal?&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
In order to count pairs, we need to know what a pair means in this situation.  Since the value of colors are different numbers, finding 2 of the same numbers will qualify a pair.  Then we can add up those pairs.&lt;/p&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Step 2: Plan
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Implement the Input Example&lt;/strong&gt;
Implementing the input example in real life helps me figure out the algorithm.  I keep asking myself how I would do it in real life.
```
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;n = 9&lt;br&gt;
ar = [10, 20, 20, 10, 10, 30, 50, 10, 20]&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;To put in a visual way, I made this simple illustration via [draw.io](draw.io)
![Imgur](https://imgur.com/3kWA88v.png)

In order for me to find pairs, I would sort this array first. 
![Imgur](https://imgur.com/dzSqVZm.png)

Now, we can easily see and count that there're 3 pairs.
![Imgur](https://imgur.com/eJ8PU40.png)

With the help of an example and visual graph, I feel like I fully understand the problem and have a good plan in mind.  Now I can start to write some pseudo-code to help me translate the logic to code. 

### Step 3: Execute
- **Write Pseudo Code**
```javascript


//Need to initiate a count variable to count pairs and return the value
//sort the given array
//loop through the sorted array 
//if the current item equals to the next item 
//then that's a pair, increment our count variable
//also increment i to skip the next item
//return the count value



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

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time to Code&lt;/strong&gt;
```javascript
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;function sockMerchant(n, ar) {&lt;br&gt;
  //Need to initiate a count variable to count pairs and return the value&lt;br&gt;
  let count = 0&lt;br&gt;
  //sort the given array&lt;br&gt;
  ar = ar.sort()&lt;br&gt;
  //loop through the sorted array &lt;br&gt;
  for (let i=0; i &amp;lt; n-1; i++) {&lt;br&gt;
    //if the current item equals to the next item &lt;br&gt;
    if(ar[i] === ar[i+1]){&lt;br&gt;
      //then that's a pair, increment our count variable&lt;br&gt;
      count++&lt;br&gt;
      //also increment i to skip the next item&lt;br&gt;
      i+=1&lt;br&gt;
    }&lt;br&gt;
  }&lt;br&gt;
  //return the count value&lt;br&gt;
  return count&lt;br&gt;
}&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
### Step 4: Reflect 
There are 2 tricky parts of this solution
1. Do not loop through the last item since we compare the current to the next item. There's no next item to the last item. and will throw an error
2. When a pair is found, we also need to increment the looping index to skip the paired item

Other reflections including time complexity: there are definitely better solutions for time complexity since I sort the array first (O(n log(n))) and a for loop (O(n)). 


This is how I solved this problem with UPER.  Documenting the thought process is fun.  I'll keep writing.  Hope you like this.
Have a great day, everyone :)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>javascript</category>
      <category>career</category>
      <category>beginners</category>
      <category>showdev</category>
    </item>
  </channel>
</rss>
