-4
List<Integer> list = new ArrayList<>(1,2,1,3,5);

output will be [2,3,5] without 1

Blauharley
  • 4,186
  • 6
  • 28
  • 47
Yagami
  • 11
  • 5
  • And where is your attempt? – Elliott Frisch Jan 06 '18 at 02:54
  • i want to know how to delete the duplicated elements in arraylist like this – Yagami Jan 06 '18 at 02:57
  • 2
    Possible duplicate of [How do I remove repeated elements from ArrayList?](https://stackoverflow.com/questions/203984/how-do-i-remove-repeated-elements-from-arraylist) – mrcrow85 Jan 06 '18 at 03:11
  • I am searching a lot , but i do not want to clear duplicated , i want to remove all duplicated from Array list like (1,2,3,1,2,5) //output : (3,5) – Yagami Jan 06 '18 at 03:12
  • i just want to delete these elements from array list finally [1,2,3,1,2,5] print 3 , 5 because these elements not duplicated – Yagami Jan 06 '18 at 03:26

3 Answers3

0

May this can help you:

import java.util.ArrayList;
import java.util.Arrays;

public class RemoveDuplicateElement {

    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<Integer>(Arrays.asList(1,2,1,3,5));
        ArrayList<Integer> duplicateElementsRemovedList = new ArrayList<Integer>();
        for (Integer elementToSearch :list) {
            int found=0;
            for (Integer element :list) {
                if(elementToSearch.equals(element)) {
                    found++;
                }
            }
            if(found==1)
            {
                duplicateElementsRemovedList.add(elementToSearch);
            }
        }
        for (Integer element :duplicateElementsRemovedList) {
            System.out.println(element);
        }

    }

}
Joby Wilson Mathews
  • 10,528
  • 6
  • 54
  • 53
-1

If you want to have only unique records you can use HashSet, however as you mentioned you want to remove all elements which are duplicates, you can use this simplest piece of code to do it, what you will need to do is create a new list which will only contain uniques and remove duplicates..take a look

List cleanList = new ArrayList<Integer>();
for(int n=0;n<list.size();n++){

  if(!cleanList.contains(list.get(n))){
    cleanList.add(list.get(n));
  }else{
    cleanList.remove(list.get(n));
  }
}

The result in cleanList will be 3, 5. See if you can use this or get ideas from the above script

user1063108
  • 662
  • 1
  • 10
  • 24
  • if you put the same element in indexes 1, 3 and 5. it will added at 1, removed at 3 and re-added at 5. (1,2,1,3,5,1) – Ctznkane525 Jan 06 '18 at 03:43
-2

You could have googled once before you posted your question here, anyway check this link out.

A part of an array cannot be deleted. You have two options, either you can shift elements to the left or create a new array without the duplicate elements and delete the old array.

This might solve your query.

Satnam Sandhu
  • 610
  • 1
  • 10
  • 25
  • I am searching a lot , but i do not want to clear duplicated , i want to remove all duplicated from Array list like (1,2,3,1,2,5) //output : (3,5) – Yagami Jan 06 '18 at 03:10
  • Sorry, couldn't get you, can u elaborate a bit more. – Satnam Sandhu Jan 06 '18 at 03:12
  • If your want to delete a part of array which are duplicate then its not possible. Check [this](https://stackoverflow.com/questions/2747111/deleting-part-of-an-array-in-java-to-free-memory-on-heap) question and this time i hope it will help for sure. :D – Satnam Sandhu Jan 06 '18 at 03:16
  • Yup i am really want to do this Clear all duplicated element form array (1,2,3,1,2,5) //output : (3,5) – Yagami Jan 06 '18 at 03:18
  • You can look at [this](https://stackoverflow.com/questions/5767325/how-do-i-remove-a-particular-element-from-an-array-in-javascript) link also. In this you can create a new array without the duplicate members and then delete the old array. But this is implemented in javascript but it can surely give an direction to proceed with. – Satnam Sandhu Jan 06 '18 at 03:18
  • is it possible with array list in java to delete these elements ? – Yagami Jan 06 '18 at 03:20
  • 2
    A link to a solution is welcome, but please ensure your answer is useful without it: [add context around the link](//meta.stackexchange.com/a/8259) so your fellow users will have some idea what it is and why it’s there, then quote the most relevant part of the page you're linking to in case the target page is unavailable. [Answers that are little more than a link may be deleted.](//stackoverflow.com/help/deleted-answers) – Papershine Jan 06 '18 at 03:25
  • Thank your suggestion i will edit my answer and will post the next answer keeping this in mind. :) – Satnam Sandhu Jan 06 '18 at 03:40