0

This is my jsp page.

<body>
    <%

        String a[] = {"PAK", "ENG", "IND", "USA"};
        String b[] = {"ON", "UK","IND","ENG","SA"};
        String[] Filterjoined = ObjectArrays.concat(a, b, String.class);
        out.println(Arrays.toString(Filterjoined));
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < Filterjoined.length; i++) {
            boolean found = false;
            for (int j = i + 1; j < Filterjoined.length; j++) {
                if (Filterjoined[j].equals(Filterjoined[i])) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                if (sb.length() > 0) {
                    sb.append(',');
                }
                sb.append(Filterjoined[i]);
            }
        }
        out.println("<br>");
        out.println(sb);
    %>
</body>

Here i'm getting output as PAK,USA,ON,UK,IND,ENG,SA but i need to delete string from both arrays if string has duplicated. i.e., expected output is:PAK,USA,ON,UK,ENG,SA because IND has duplicated in both arrays so i need to delete it,remaining elements has to display.Thanks for your reply

4
  • 2
    Easy way is to create Set and put all items in it. Set does not store duplicates. Commented May 8, 2014 at 6:10
  • Isn't "ENG" present in both arrays as well? Commented May 8, 2014 at 6:11
  • How can i use Set for this code ,could you please change my code if possible @Leos Commented May 8, 2014 at 6:13
  • 2
    Replace your code by response.sendRedirect("http://docs.oracle.com/javase/tutorial/collections/"); Commented May 8, 2014 at 6:16

2 Answers 2

4

This is how to use the set.

Set<String> joined = new HashSet<String>(Arrays.asList(a));
joined.addAll(Arrays.asList(b));
String[] joinedArray = joined.toArray(new String[joined.size()]);
Sign up to request clarification or add additional context in comments.

Comments

0

Java already has everything you need to do your task without third party libraries, etc. Use the following solution:

String a[] = {"PAK", "ENG", "IND", "USA"};
String b[] = {"ON", "UK","IND","ENG","SA"};

/* Convert your arrays to lists */
final List<String> listA = Arrays.asList(a);
final List<String> listB = Arrays.asList(b);

/* Create another list holding 'duplicate' elements */
final List<String> duplicatesList = new ArrayList<String>(listA);
duplicatesList.retainAll(listB);

/* Finally create a resulting list as a + b - duplicates */
final List<String> result = new ArrayList<String>(listA);
result.addAll(listB);
result.removeAll(duplicatesList);

System.out.println(result);

4 Comments

how can i remove brackets [,] from result string
You cannot literally remove values from arrays in java 'cause arrays are fixed-size. In your case you can just reassign your array: final List<String> newA = new ArrayList<String>(listA);, then newA.removeAll(duplicatesList); and finally a = newA.toArray(a); But I'd suggest you using lists instead of arrays if such manipulations appear in your code often...
just i need the result as:PAK, ON, UK .but i'm getting [PAK,ON,UK].Could you please tell me where to change the code
Replace System.out.println(result); with the desired implementation of printing the results :) You can use your code with StringBuilder from the original question or apache's StringUtils: System.out.println(StringUtils.join(result, ", "));.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.