-3

I have a matrix that has a set of elements and duplicates I want to add items to ArrayList with no duplicate elements

 package window1;
 import java.util.ArrayList;
  public class Mine {
   public static void main(String[] args) {
     ArrayList<String> listName = new ArrayList<>();

       String sal[]={"val","sa","de","dal","val","sa","de"};

       for(int i =0;i<sal.length;i++){

          listName.add(sal[i]);
                    }

      for(int j=0;j<listName.size();j++){
         System.out.println(listName.get(j));

                    }

            }

      }
salem715
  • 19
  • 7
  • 3
    You can use `HashSet` instead which avoids duplicates –  Apr 19 '18 at 05:46
  • This questions has been answered here https://stackoverflow.com/questions/17967114/how-to-efficiently-remove-duplicates-from-an-array-without-using-set – pcgben Apr 19 '18 at 05:47
  • Thank you all of the solutions that were developed were good but using HashSet was more accurate and more effective for me Especially when you use it to filter it on BaseAdapter in Android Studio – salem715 Apr 20 '18 at 02:20

6 Answers6

1

You can simply verify if the item exists in your list before adding it, using the contains() method.

The java.util.ArrayList.contains(Object) method returns true if this list contains the specified element.

package window1;

import java.util.ArrayList;

public class Mine {
private ArrayList<String> listName = new ArrayList<>();
private String sal[]={"val","sa","de","dal","val","sa","de"};

     public static void main(String[] args) {

        for(int i =0;i<sal.length;i++)
        {

             if(!listname.contains(sal[i])) // If the element does not exist...
                 listName.add(sal[i]); // Add him
        }

       for(int j=0;j<listName.size();j++) System.out.println(listName.get(j));
    }
}
Héctor M.
  • 2,302
  • 4
  • 17
  • 35
0

You can use contains method :

if(!listName.contains(sal[i]))
                listName.add(sal[i]);

OUTPUT

val
sa
de
dal

(!listName.contains(sal[i]))

contains will return if item already in list and using ! negating the condition.

Roushan
  • 4,074
  • 3
  • 21
  • 38
0
Set<String> set = new HashSet<String>(listName);

System.out.println("Set values .....");
for (String temp : set) System.out.println(temp);
António Ribeiro
  • 4,129
  • 5
  • 32
  • 49
0

Use Hashset to remove duplicates

Set<String> noDupSet = new HashSet<>(Arrays.asList(new String[] {"val1","val2","val1"}));
Marco A.
  • 43,032
  • 26
  • 132
  • 246
0

Either you can use HashSet instead of ArrayList. Example:

HashSet<String> set=new HashSet<String>();  
  set.add("Ravi");  
  set.add("Vijay");  
  set.add("Ravi");  
  set.add("Ajay");  
  //Traversing elements  
  Iterator<String> itr=set.iterator();  
  while(itr.hasNext()){  
   System.out.println(itr.next());  
  }  

It will display Ravi, Vijay and Ajay as output. Note Ravi added twice but it adds only once because HashSet contains unique elements only.

Or

If you want to add unique elements in ArrayList then you will need to add items in HashSet first then you can add its item in ArrayList as shown below:

import java.util.ArrayList;
import java.util.HashSet;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ArrayList<String> listName = new ArrayList<String>();
        HashSet<String> newListName = new HashSet<String>();

        String sal[]={"val","sa","de","dal","val","sa","de"};

        // add unique items in HashSet(newListName)
        for(int i=0; i<sal.length; i++){
            newListName.add(sal[i]);
        }

        // add items of HashSet into ArrayList(listName)
        for(String s: newListName) {
            listName.add(s);
        }

        // display ArrayList(listName)
        for(String s: listName) {
            System.out.println(s);
        }
    }
}
Nitin Bisht
  • 5,053
  • 4
  • 14
  • 26
0

First convert your array to hashSet.Which will remove the duplicates.

Next convert the HashSet to ArrayList.(Your desired Data Structure collection)

Check The Code bellow.

public static void main(String[] args)
{
        String sal[]={"val","sa","de","dal","val","sa","de"};
        HashSet<String> ss = new HashSet(Arrays.asList(sal));
        System.out.println("HashSet-");
        System.out.println(ss);
        ArrayList<String> asl= new ArrayList<String>(ss);
        System.out.println("ArrayList-");
        System.out.println(asl);
}

O/P:

HashSet-
[de, val, sa, dal]
ArrayList-
[de, val, sa, dal]