java – Remove the duplicate pair from List

i was working on one issue i am not getting the expected output.

A String array “F1” has got names of Facebook users and their friend association.

For example, if we write: U1,U2 it implies that U1 is a friend of U2. This also implies that U2 is a friend of U1.
Write a program which will read “F1” and remove duplicates and write all the unique pairs to “F2”.
But, before removing duplicates

Input String => [“U1,U2″,”U3,U4″,”U2,U1″,”U1,U5”]

Output String => [“U1,U2″,”U1,U5″,”U3,U4”]

public static void main(String args[]) {

    List test = new ArrayList();
    List<String> list = new ArrayList();
    list.add("U1,U2");
    list.add("U3,U4");
    list.add("U2,U1");
    list.add("U1,U5");
    Collections.sort(list);

    for (String str : list) {
        String i1 = str.substring(0, 2);
        String i2 = str.substring(3, 5);
         System.out.println(i2);
         if (!i1.equals(i2)) {
         test.add(str);
         }
        if (!(str.contains(i1) && str.contains(i2)) || !(str.contains(i2) && str.contains(i1))) {
            System.out.println(str);
        }


    }

}

}

Loading...

(Visited 194 times, 1 visits today)