public class Main {
/**
- Iterate through each line of input.
*/
public static void main(String args) throws IOException {
InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
BufferedReader in = new BufferedReader(reader);
String line;
String parts;
while ((line = in.readLine()) != null) {
line = line.trim();
parts = line.split(“;”);
System.out.println(
findIntersection(
toIntArray(parts[0].split(“,”)),
toIntArray(parts[1].split(“,”))
)
);
}
}
private static String findIntersection(int input1, int input2){
Set intersection = new LinkedHashSet<>();
for (int i = 0, j = 0; i < input1.length; i++){
while (input1[i] > input2[j] && j < input2.length){
j++;
}
if (input1[i] < input2[j]){
continue;
}
else {
intersection.add(input2[j++]);
}
}
if (intersection.isEmpty()){
return "";
}
String result = intersection.toString();
result = result.trim();
return result.substring(1, result.length() -1);
}
private static int toIntArray(String array){
int nums = new int[array.length];
for (int i = 0; i < array.length; i++){
nums[i] = Integer.parseInt(array[i]);
}
return nums;
}
}
Output:
{1,2,3,4;3,4,5,6}
3, 4
Expected Output:
{1,2,3,4;3,4,5,6}
3,4
Hi, i need help with the code as the output have white space infront, i try removing the whitespace with trim() but it does not have any effect