When I run this activity, it records the visible list objects to an array, but when I scroll down and go back up, it duplicates the elements again into the array. How to fix it? For an item to be added only once.

I have tried **set** but getting the same issue.

```
private void displayMessages(){

    final Set<ChatMessage> mSet= new HashSet<ChatMessage>();
    mChat = new ArrayList<>();

    mDBRootRef.child("messages").child(messageSenderId).child(messageReceiverId)
            .addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                    for (DataSnapshot snapshot : dataSnapshot.getChildren() ) {
                        ChatMessage chat = snapshot.getValue(ChatMessage.class);

                        mSet.add(chat);

                    }

                    mChat.clear();
                    mChat.addAll(mSet);

                    MessageAdapter messageAdapter = new MessageAdapter(ChatActivity.this, mChat);
                    mUserMessagesListRV.setAdapter(messageAdapter);

                }

```

I have also tried ***.addChildEventListener*** instead of .***addValueEventListener*** but getting the same issue.

Here is recycyleview initialisation. 

```
    mUserMessagesListRV = (RecyclerView) findViewById(R.id.recyclerview_chat_log);
    mUserMessagesListRV.setHasFixedSize(true);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
    linearLayoutManager.setStackFromEnd(true);
    mUserMessagesListRV.setLayoutManager(linearLayoutManager);

```

Here is onBindViewHolder method.

```
@Override
public void onBindViewHolder(@NonNull MessageAdapter.ViewHolder viewHolder, int position) {

    //mChat is a list declared as private List<ChatMessage> mChat;
    ChatMessage chat = mChat.get(position);
    viewHolder.showMessage.setText(chat.getMessageText());
}

```