Skip to main content

This should work faster:

private void findUnCommon{

    Set<Integer> a = new HashSet<>(Arrays.asList(1, 2, 3, 4));
    Set<Integer> b = new HashSet<>(Arrays.asList(3, 4, 5, 6));

    Set<Integer> result = new HashSet<>();
    for (Integer el: a) {
      if (!b.contains(el)) {
        result.add(el);
      }
    }
    for (Integer el: b) {
      if (!a.contains(el)) {
        result.add(el);
      }
    }
    System.out.println("Uncommon elements of set a and set b is : "
        + result);
}

This should work faster:

private void findUnCommon{

    Set<Integer> a = new HashSet<>(Arrays.asList(1, 2, 3, 4));
    Set<Integer> b = new HashSet<>(Arrays.asList(3, 4, 5, 6));

    Set<Integer> result = new HashSet<>();
    for (Integer el: a) {
      if (!b.contains(el) {
        result.add(el);
    }
    for (Integer el: b) {
      if (!a.contains(el) {
        result.add(el);
    }
    System.out.println("Uncommon elements of set a and set b is : "
        + result);
}

This should work faster:

private void findUnCommon{

    Set<Integer> a = new HashSet<>(Arrays.asList(1, 2, 3, 4));
    Set<Integer> b = new HashSet<>(Arrays.asList(3, 4, 5, 6));

    Set<Integer> result = new HashSet<>();
    for (Integer el: a) {
      if (!b.contains(el)) {
        result.add(el);
      }
    }
    for (Integer el: b) {
      if (!a.contains(el)) {
        result.add(el);
      }
    }
    System.out.println("Uncommon elements of set a and set b is : "
        + result);
}
Source Link

This should work faster:

private void findUnCommon{

    Set<Integer> a = new HashSet<>(Arrays.asList(1, 2, 3, 4));
    Set<Integer> b = new HashSet<>(Arrays.asList(3, 4, 5, 6));

    Set<Integer> result = new HashSet<>();
    for (Integer el: a) {
      if (!b.contains(el) {
        result.add(el);
    }
    for (Integer el: b) {
      if (!a.contains(el) {
        result.add(el);
    }
    System.out.println("Uncommon elements of set a and set b is : "
        + result);
}