Skip to content

Instantly share code, notes, and snippets.

@h2rashee
Last active October 11, 2015 05:43
Show Gist options
  • Save h2rashee/3500f55dd4a7c97600e5 to your computer and use it in GitHub Desktop.
Save h2rashee/3500f55dd4a7c97600e5 to your computer and use it in GitHub Desktop.
Determine the symmetric difference of multiple lists of numbers
/*
Given a multiple sorted lists, determine the unique elements (symmetric difference).
https://en.wikipedia.org/wiki/Symmetric_difference
*/
import java.util.*;
class DoubleList
{
public static void main (String [] args)
{
Scanner sc = new Scanner(System.in);
int[] a = new int[sc.nextInt()];
for(int i = 0; i < a.length; i++) {
a[i] = sc.nextInt();
}
int[] b = new int[sc.nextInt()];
for(int i = 0; i < b.length; i++) {
b[i] = sc.nextInt();
}
ArrayList<Integer> ans = symmetricDiff(a, b);
for(int i = 0; i < ans.size(); i++) {
System.out.print(ans.get(i) + " ");
}
}
// Two list case
public static ArrayList<Integer> symmetricDiff(int[] a, int[] b) {
int i = 0;
int j = 0;
ArrayList<Integer> ans = new ArrayList<Integer>();
while(i < a.length && j < b.length) {
if(a[i] == b[j]) {
i++;
j++;
} else if(a[i] < b[j]) {
ans.add(a[i]);
i++;
} else {
ans.add(b[j]);
j++;
}
}
for(; i < a.length; i++) {
ans.add(a[i]);
}
for(; j < b.length; j++) {
ans.add(b[j]);
}
return ans;
}
// Multiple list case
public static int symmetricDiff(ArrayList<int[]> llist) {
// List of counters
int[] i = new int[llist.length];
for(int j = 0; j < llist.length(); j++) {
i[j] = 0;
}
// TODO Solution
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment