title: Can we solve this Sock Merchant problem in less complexity?
body: <p>I have solved the hackerrank Sock Merchant problem But I want to <strong>reduce the complexity of the code</strong>(I am not sure that it is possible or not).</p>
  
  <blockquote>
    <p>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.</p>
    
    <p>For example, there are <strong>n=7</strong> socks with colors <strong>ar= [1,2,1,2,1,3,2]</strong>. There is one pair of color  <strong>1</strong> and one of color <strong>2</strong>. There are three odd socks left, one of each color. The number of pairs is 2.</p>
    
    <p><strong>Function Description</strong></p>
    
    <p>Complete the sockMerchant function in the editor below. It must return an integer representing the number of matching pairs of socks that are available.</p>
    
    <p>sockMerchant has the following parameter(s):</p>
    
    <ul>
    <li><p>n: the number of socks in the pile</p></li>
    <li><p>ar: the colors of each sock</p></li>
    </ul>
    
    <p><strong>Input Format</strong></p>
    
    <p>The first line contains an integer <strong>n</strong>, the number of socks represented in <strong>ar</strong>.
    The second line contains <strong>n</strong> space-separated integers describing the colors <strong>ar[i]</strong> of the socks in the pile.</p>
    
    <p><strong>Constraints</strong></p>
    
    <ul>
    <li><p>1 &lt;= n &lt;= 100</p></li>
    <li><p>1 &lt;= ar[i] &lt;= 100  where 0 &lt;= i &lt; n</p></li>
    </ul>
    
    <p>Output Format</p>
    
    <p>Return the total number of matching pairs of socks that John can sell.</p>
    
    <p>Sample Input</p>
  </blockquote>
  
  <pre><code>9
  10 20 20 10 10 30 50 10 20
  </code></pre>
  
  <blockquote>
    <p>Sample Output</p>
  </blockquote>
  
  <pre><code>3
  </code></pre>
  
  <blockquote>
    <hr>
    
    <p>My solutions :</p>
  </blockquote>
  
  <pre><code>package com.hackerrank.test;
  
  public class Solution {
      public static void main(String[] args) {
          //Initialize array
          int[] arr = new int[]{10, 20, 20, 10, 10, 30, 50, 10, 20};
          //Array fr will store frequencies of element
  
  
          System.out.println("---------------------------------------");
          System.out.println(" sockMerchant output " + sockMerchant(9, arr));
          System.out.println("---------------------------------------");
  
      }
  
      static int sockMerchant(int n, int[] ar) {
          int pairs = 0;
          int frequencyArray[] = new int[ar.length];
          int frequencyTemp = -1;
          for (int i = 0; i &lt; ar.length; i++) {
              int count = 1;
              for (int j = i + 1; j &lt; ar.length; j++) {
                  if (ar[i] == ar[j]) {
                      count++;
                      frequencyArray[j] = frequencyTemp;
                  }
              }
              if (frequencyArray[i] != frequencyTemp) {
                  frequencyArray[i] = count;
              }
          }
  
          for (int i = 0; i &lt; frequencyArray.length; i++) {
              if (frequencyArray[i] != frequencyTemp) {
                  int divide = frequencyArray[i] / 2;
                  pairs += divide;
              }
          }
          return pairs;
      }
  }
  </code></pre>
  
  <p>And the output is : </p>
  
  <pre><code>    ---------------------------------------
      sockMerchant frequency 3
      ---------------------------------------
  </code></pre>