how to select unique contacts from android

I think you mean you got duplicate record for some contacts. So you must add condition for your query. The essential part is contacts must be in visible group and have phone number.

String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '"
                + ("1") + "'";
        String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
                + " COLLATE LOCALIZED ASC";
cur = context.getContentResolver().query(
                ContactsContract.Contacts.CONTENT_URI, projection, selection
                        + " AND " + ContactsContract.Contacts.HAS_PHONE_NUMBER
                        + "=1", null, sortOrder);// this query only return contacts which had phone number and not duplicated

Update 20/05/2020

  suspend fun fetchContacts(): ArrayList<FriendItem> {
        val list = ArrayList<FriendItem>()
        val uri: Uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI
        val selection = ContactsContract.Contacts.HAS_PHONE_NUMBER
        val cursor: Cursor? = context.contentResolver.query(
            uri,
            arrayOf(
                ContactsContract.CommonDataKinds.Phone.NUMBER,
                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Phone._ID,
                ContactsContract.Contacts._ID
            ),
            selection,
            null,
            ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC"
        )

        cursor?.let {
            val nameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)
            val phoneIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)
            while (cursor.moveToNext()) {
                val info = FriendItem(
                    friendName = cursor.getString(nameIndex),
                    friendPhoneNumber = cursor.getString(phoneIndex)
                )
                list.add(info)
            }
            cursor.close()
        }
        return list
    }

This is working for me to get contact with phone number. Here we are querying Data table, and using CONTACT_ID contact provider documentation

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {

final String ORDER_BY = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME_PRIMARY + " ASC";

    final String[] PROJECTION = {
            ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
            ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME_PRIMARY,
            ContactsContract.CommonDataKinds.Phone.NUMBER
    };

return new CursorLoader(
                context,
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                PROJECTION,
                null,
                null,
                ORDER_BY
        );
}