-1

I'm a beginner,I tried to find duplicate elements in the given list but for some reason I'm getting the element 3 printed twice. Can anyone help me out?

OUTPUT:

1 1 2 2 3 3 3 3 3 3 3 4 4 4 5 5 6 6

Duplicate element(s)

1 2 3 3 3 3 4 5 6

3 4

Duplicate element(s) removed

1 2 3 3 4 5 6

public class Duplicate {
    public static void main(String[] args) {        
        int[] array={1,2,2,3,3,4,5,5,3,4,3,4,6,6,1,3,3,3};  
        Arrays.sort(array);             
        List<Integer> list=new ArrayList<Integer>();
        for(int x:array)
            list.add(x);
        for(int i=0;i<list.size();i++)
            System.out.print(list.get(i)+" ");
        System.out.println("\nDuplicate element(s)");
        for(int i=0;i<list.size();i++){
            for(int j=i+1;j<list.size();j++){
                if((list.get(i)==list.get(j))){
                    System.out.print(list.get(j)+" ");
                    list.remove(j);                     
                }               
            }
        }
        System.out.println();
        for(int i=0;i<list.size();i++){
            for(int j=i+1;j<list.size();j++){
                if((list.get(i)==list.get(j))){
                    System.out.print(list.get(j)+" ");
                    list.remove(j);     
                }
            }
        }       
        System.out.println("\nDuplicate element(s) removed");       
            for(int i=0;i<list.size();i++)
            System.out.print(list.get(i)+" ");
    }
}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
suze
  • 3
  • 6
  • I'm not sure, but think this is a duplicate of http://stackoverflow.com/questions/21985397/removing-duplicate-values-from-arraylist – Marco13 Aug 20 '16 at 13:30

5 Answers5

1

You can use collections but the thing is, you will need to wrap the primitives with the Integer class... btw using jdk 8 will be more easy

   public static void main(String[] args) {
    Integer[] array = { 6, 1, 2, 2, 3, 3, 4, 5, 5, 3, 4, 3, 4, 6, 6, 1, 3, 3, 3 };
    Set<Integer> setJder = new LinkedHashSet<>(Arrays.asList(array));
    System.out.println(setJder );


    //  since JDK 8
    Set<Integer> mySet = Stream.of(6, 1, 2, 2, 3, 3, 4, 5, 5, 3, 4, 3, 4, 6, 6, 1, 3, 3, 3)
        .collect(Collectors.toCollection(HashSet::new));
    System.out.println(mySet);
    }
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

using set you can do this.

Integer[] arr = {1,2,2,3,3,4,5,5,3,4,3,4,6,6,1,3,3,3};
Set<Integer> mySet = new HashSet<Integer>();
    Collections.addAll(mySet, arr);
    System.out.println(mySet); 
Durgpal Singh
  • 11,481
  • 4
  • 37
  • 49
0

Try with this:

public static void main(String[] args) {

    int[] array = {1, 2, 2, 3, 3, 4, 5, 5, 3, 4, 3, 4, 6, 6, 1, 3, 3, 3};
    Arrays.sort(array);
    List<Integer> list = new ArrayList<>();
    for (int x : array) {
        list.add(x);
    }
    for (int i = 0; i < list.size(); i++) {
        System.out.print(list.get(i) + " ");
    }
    System.out.println();
    System.out.println("\nDuplicate element(s)");
    Integer last = null;
    Iterator<Integer> it = list.iterator();
    while (it.hasNext()){
        Integer n = it.next();
        if (n.equals(last)){
            System.out.print(n + " ");
            it.remove();
        }
        last = n;
    }
    System.out.println();

    System.out.println("\nDuplicate element(s) removed");

    for (int i = 0; i < list.size(); i++) {
        System.out.print(list.get(i) + " ");
    }
    System.out.println();
}

EDIT:

    System.out.println("\nDuplicate element(s)");
    for (int i = 0; i < list.size(); i++) {
        for (; (i+1) < list.size() && (list.get(i).equals(list.get(i+1)));) {
            System.out.print(list.get(i+1) + " ");
            list.remove(i+1);
        }
    }
    System.out.println();
David Pérez Cabrera
  • 4,960
  • 2
  • 23
  • 37
0

If you don't want duplicates in a Collection, you should consider why you're using a Collection that allows duplicates. The easiest way to remove repeated elements is to add the contents to a Set (which will not allow duplicates) and then add the Set back to the ArrayList:

ArrayList<Integer> al = new ArrayList<Integer>(Arrays.asList(array));
Set<Integer> hs = new HashSet<Integer>();
hs.addAll(al);
al.clear();
al.addAll(hs);

But this destroys the ordering of the elements in the ArrayList.

jarvo69
  • 7,908
  • 2
  • 18
  • 28
0

Another way to do this is using counting sort logic.Or as the previous comments state using a Hash Set,or Tree set,depending on whether you need them ordered.But if you use a set ,remember to check if the .hashCode() and .equals() work properly