private static String [ ] differences ( String [ ] first , String [ ] second ) { String [ ] sortedFirst = Arrays . copyOf ( first , first . length ) ; // O(n) String [ ] sortedSecond = Arrays . copyOf ( second , second . length ) ; // O(m) Arrays . sort ( sortedFirst ) ; // O(n log n) Arrays . sort ( sortedSecond ) ; // O(m log m) int firstIndex = 0 ; int secondIndex = 0 ; LinkedList < String > diffs = new LinkedList <> ( ) ; while ( firstIndex < sortedFirst . length && secondIndex < sortedSecond . length ) { int compare = ( int ) Math . signum ( sortedFirst [ firstIndex ] . compareTo ( sortedSecond [ secondIndex ] ) ) ; switch ( compare ) { case - 1 : diffs . add ( sortedFirst [ firstIndex ] ) ; firstIndex ++ ; break ; case 1 : diffs . add ( sortedSecond [ secondIndex ] ) ; secondIndex ++ ; break ; default : firstIndex ++ ; secondIndex ++ ; } } String [ ] strDups = new String [ diffs . size ( ) ] ; return diffs . toArray ( strDups ) ; }
Returns an array that contains all differences between first and second .