-1

I have problem in completely removing the duplicates string in List<String>

String [] myno1 = new String [] {"01", "02", "03", "04", "05", "06",
"07", "08", "09", "10", "11", "12", "13", "14", "15"};

String [] myno = new String [] {"01", "03", "15"};

List<String> stringList = new ArrayList<String>(Arrays.asList(myno));

List<String> stringList1 = new ArrayList<String>(Arrays.asList(myno1));

stringList.addAll(stringList1);

Set<String> set = new HashSet<>(stringList);
stringList.clear();
stringList.addAll(set);

System.out.println("=== s:" +stringList);

but i got this:

=== s:[15, 13, 14, 11, 12, 08, 09, 04, 05, 06, 24, 07, 01, 02, 03, 10]

i want the result to be like this:

=== s:[13, 14, 11, 12, 08, 09, 04, 05, 06, 24, 07, 02, 10]

Andrew Trubin
  • 143
  • 1
  • 1
  • 11
twoseven
  • 3
  • 1
  • 3
    Can you clarify what exactly you want to do? Do you want to create a list that combines "myno" and "myno1" in such a way that it contains everything except the items contained in both lists (i.e. a xor operation)? – FileNotFoundEx Jul 28 '19 at 05:40

3 Answers3

0

As you have to remove the items from another list. HashSet<>() is used to remove the duplicate from the same array list. e.g if the list contains 15 two times and 3 two times then it will remain single time in the list.

Here is the code

foreach(String str : stringList){

stringList1.remove(str);
}
Sunny
  • 3,134
  • 1
  • 17
  • 31
0

Take it:

String[] myno1 = new String[]{"01", "02", "03", "04", "05", "06", "07", 
                              "08", "09", "10", "11", "12", "13", "14", "15"};
String[] myno2 = new String[]{"01", "03", "15"};

// use LinkedHashSet to preserve order
Set<String> set1 = new LinkedHashSet<>(Arrays.asList(myno1));
Set<String> set2 = new LinkedHashSet<>(Arrays.asList(myno2));

// find duplicates
Set<String> intersection = new LinkedHashSet<>();
intersection.addAll(set1);
intersection.retainAll(set2);

// remove duplicates from both sets
Set<String> result = new LinkedHashSet<>();
result.addAll(set1);
result.addAll(set2);
result.removeAll(intersection);

System.out.println("Result: " + result);

Result: [02, 04, 05, 06, 07, 08, 09, 10, 11, 12, 13, 14]
Bor Laze
  • 2,458
  • 12
  • 20
0

If you use Java 8 or +, you can use this:

String[] myno1 = new String[] { "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15" };
String[] myno = new String[] { "01", "03", "15" };
List<String> stringList = new ArrayList<>(Arrays.asList(myno));
List<String> stringList1 = new ArrayList<>(Arrays.asList(myno1));
stringList.addAll(stringList1);

List<String> newList = stringList.stream()
            .filter(string -> Collections.frequency(stringList, string) == 1)
            .collect(Collectors.toList());
System.out.println("=== s:" + newList);

Not needed to change your code that much. Made a new List where no duplicate element inserted. Output is:

=== s:[02, 04, 05, 06, 07, 08, 09, 10, 11, 12, 13, 14]
Mukit09
  • 2,956
  • 3
  • 24
  • 44