title: Few (specific everytime) contacts duplicated during insert in android
body: <p>I am trying to insert a list of contacts in android using Contact API. But I am facing a strange problem with that. Some of the contact is getting duplicated if they exist in the phone book. But this happens to only a few of the contacts not to all the contacts which confused me more.</p>
  
  <p>Here is my code.</p>
  
  <pre><code>final ArrayList&lt;ContentProviderOperation&gt; restoredContactList = new ArrayList&lt;ContentProviderOperation&gt;();
  restoredContactList.clear();
  restoredContactList
  .add(ContentProviderOperation
      .newInsert(
          ContactsContract.RawContacts.CONTENT_URI)
      .withValue(
          ContactsContract.RawContacts.ACCOUNT_TYPE,
          mAccountType)
      .withValue(
          ContactsContract.RawContacts.ACCOUNT_NAME,
          mAccountName).build());
  
  for (int j = 0; j &lt; namesArray.length(); j++) {
      final JSONObject nameObj = namesArray
      .getJSONObject(j);
  
      restoredContactList
      .add(ContentProviderOperation
          .newInsert(
              ContactsContract.Data.CONTENT_URI)
          .withValueBackReference(
              ContactsContract.Data.RAW_CONTACT_ID,
              0)
          .withValue(
              ContactsContract.Data.MIMETYPE,
              ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
          .withValue(
              ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,
              nameObj.getString("first_name")
              + " "
              + nameObj
              .getString("last_name"))
          .withValue(
              ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,
              nameObj.getString("first_name"))
          .withValue(
              ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME,
              nameObj.getString("last_name"))
          .build());
  }
  
  // Phone
  final JSONArray phonesArray = obj
  .getJSONArray("phones");
  for (int j = 0; j &lt; phonesArray.length(); j++) {
  
      final JSONObject phoneObj = phonesArray
      .getJSONObject(j);
  
      restoredContactList
      .add(ContentProviderOperation
          .newInsert(
              ContactsContract.Data.CONTENT_URI)
          .withValueBackReference(
              ContactsContract.Data.RAW_CONTACT_ID,
              0)
          .withValue(
              ContactsContract.Data.MIMETYPE,
              ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
          .withValue(
              ContactsContract.CommonDataKinds.Phone.NUMBER,
              phoneObj.getString("phone_number"))
          .withValue(
              ContactsContract.CommonDataKinds.Phone.TYPE,
              getTypeOfNumber(phoneObj
                  .getString("phone_type")))
          .build());
  }
  
  // Email
  final JSONArray emailsArray = obj
  .getJSONArray("emails");
  for (int j = 0; j &lt; emailsArray.length(); j++) {
  
      final JSONObject mailObj = emailsArray
      .getJSONObject(j);
  
      restoredContactList
      .add(ContentProviderOperation
          .newInsert(
              ContactsContract.Data.CONTENT_URI)
          .withValueBackReference(
              ContactsContract.Data.RAW_CONTACT_ID,
              0)
          .withValue(
              ContactsContract.Data.MIMETYPE,
              ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
          .withValue(
              ContactsContract.CommonDataKinds.Email.DATA,
              mailObj.getString("email_address"))
          .withValue(
              ContactsContract.CommonDataKinds.Email.TYPE,
              getTypeOfEmail(mailObj
                  .getString("email_type")))
          .build());
  }
  
  // Address
  final JSONArray addressesArray = obj
  .getJSONArray("address");
  for (int j = 0; j &lt; addressesArray.length(); j++) {
      final JSONObject addressObj = addressesArray
      .getJSONObject(j);
  
      restoredContactList
      .add(ContentProviderOperation
          .newInsert(
              ContactsContract.Data.CONTENT_URI)
          .withValueBackReference(
              ContactsContract.Data.RAW_CONTACT_ID,
              0)
          .withValue(
              ContactsContract.Data.MIMETYPE,
              ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
          .withValue(
              ContactsContract.CommonDataKinds.StructuredPostal.STREET,
              addressObj
              .getString("street"))
          .withValue(
              ContactsContract.CommonDataKinds.StructuredPostal.CITY,
              addressObj
              .getString("city"))
          .withValue(
              ContactsContract.CommonDataKinds.StructuredPostal.REGION,
              addressObj
              .getString("state"))
          .withValue(
              ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE,
              addressObj
              .getString("zipcode"))
          .withValue(
              ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY,
              addressObj
              .getString("country"))
          .withValue(
              ContactsContract.CommonDataKinds.StructuredPostal.TYPE,
              getTypeOfAddress(addressObj
                  .getString("addressType")))
          .build());
  }
  
  // Asking the Contact provider to create a new
  // contact
  getContentResolver().applyBatch(
      ContactsContract.AUTHORITY,
      restoredContactList);
  </code></pre>
  
  <p>I am having 128 contacts in my device already.</p>
  
  <p>Basically app is to restore contacts from the server. First time when i restored all contact at that time i deleted all the existing contact from my phone book, it works fine. Now in my phone I am having 128 contacts and I am trying to restore contacts again,but now I got 133 contacts, so 5 of contacts are duplicated in this case. I don't know why is it happening.</p>
  
  <p>I am not getting any error or exception. I think i am making some small mistake,but not able to recognize it. If anyone has any idea,please kindly help me.</p>