title: How to dynamically remove duplicate JSON Object from JSON String array
body: <p>I have got the JSON as below</p>
  
  <pre><code>{
      "brands": [
          {
              "name": "ACC",
              "quantity": "0",
              "listedbrandID": 1,
              "status": "0"
          }
      ],
      "others": [
          {
              "name": "dd",
              "quantity": "55",
       "listedbrandID": 1
  
          },
          {
              "name": "dd",
              "quantity": "55",
       "listedbrandID": 1
  
          }
  
      ]
  }
  </code></pre>
  
  <p>I am trying to remove the dupcate JSON objects from the Others array </p>
  
  <p>I was trying it this way </p>
  
  <pre><code>    String JSON = "{
      "brands": [
          {
              "name": "ACC",
              "quantity": "0",
              "listedbrandID": 1,
              "status": "0"
          }
      ],
      "others": [
          {
              "name": "dd",
              "quantity": "55",
       "listedbrandID": 1
  
          },
          {
              "name": "dd",
              "quantity": "55",
       "listedbrandID": 1
  
          }
  
      ]
  }" ;
  
  
          JSONObject json_obj = new JSONObject(json);
          JSONArray array = json_obj.getJSONArray("others");
          HashSet&lt;Others&gt; map = new HashSet&lt;Others&gt;();
          for (int k = 0; k &lt; array.length(); k++) {
              Others otherbrands = new Others();
              String name = array.getJSONObject(k).getString("name");
              String quantity = array.getJSONObject(k).getString("quantity");
              String listedbrandID = array.getJSONObject(k).getString("listedbrandID");
              if(name!=null &amp;&amp; !name.trim().equals(""))
              {
              otherbrands.setName(name);
              otherbrands.setQuantity(quantity);
              otherbrands.setListedbrandID(listedbrandID);
              map.add(otherbrands);
              }
          }
          for (int k = 0; k &lt; array.length(); k++) 
          {
              String name = array.getJSONObject(k).getString("name");
              System.out.println(name);
              if(!map.contains(name.trim()))
              {
                  array.remove(k);
              }
          }
          System.out.println(json_obj);
      }
  }
  
  
  import java.util.Arrays;
  
  
  public class Others {
  
      private String name ;
      public String getName() {
          return name;
      }
      public void setName(String name) {
          this.name = name;
      }
      public String getQuantity() {
          return quantity;
      }
      public void setQuantity(String quantity) {
          this.quantity = quantity;
      }
      public String getListedbrandID() {
          return listedbrandID;
      }
      public void setListedbrandID(String listedbrandID) {
          this.listedbrandID = listedbrandID;
      }
      private String quantity ;
      private String listedbrandID ;
  
       @Override
          public boolean equals (Object other)
          {
              if (!(other instanceof Others))
                  return false;
              Others ob = (Others) other;
              return name.equals(ob.name) &amp;&amp;
              quantity.equals(ob.quantity);
          }
  
          @Override
          public int hashCode ()
          {
              return Arrays.hashCode(new String[]{quantity,name});
          }
  
  
  
  }
  </code></pre>
  
  <p>But its printing the entire JSON</p>