My program gets the symmetric difference between two lists. Here is my code:
import java.util.*;
/**
* A class to find the symmetric difference between two lists.
*/
public class SymDifference
{
/**
* Finds the symmetric difference between two sorted lists.
*
* @param L1 first list
* @param L2 second list
* @param Result list with the symmetric difference
*/
static <AnyType extends Comparable<? super AnyType>> void symDifference(List<AnyType> L1, List<AnyType> L2,
List<AnyType> Result)
{
//create two iterators to go through the list
ListIterator<AnyType> iterL1 = L1.listIterator();
ListIterator<AnyType> iterL2 = L2.listIterator();
//create two anytype objs
AnyType itemL1 = null;
AnyType itemL2 = null;
//gets the first two items of the lists
if (iterL1.hasNext() && iterL2.hasNext())
{
itemL1 = iterL1.next();
itemL2 = iterL2.next();
}
//use a while loop to compare elements of lists
while (itemL1 != null && itemL2 != null)
{
int compareResult = itemL1.compareTo(itemL2);
//elements are the same so go on to the next items
if (compareResult == 0)
{
//get next item for list L1
if (iterL1.hasNext())
{
itemL1 = iterL1.next();
}
else
{
itemL1 = null;
}
//get next item for list L2
if (iterL2.hasNext())
{
itemL2 = iterL2.next();
}
else
{
itemL2 = null;
}
}
// the item of L1 comes after the item of L2, add item from L2 to results
else if (compareResult < 0)
{
Result.add(itemL1);
//get next item for list L1
if (iterL1.hasNext())
{
itemL1 = iterL1.next();
}
//get next item for list L2
else
{
itemL1 = null;
}
}
// the item of L1 comes before the item of L2, add item from L1 to results
else
{
Result.add(itemL2);
//get next item for list L1
if (iterL2.hasNext())
{
itemL2 = iterL2.next();
}
//get next item for list L2
else
{
itemL2 = null;
}
}
}
//add remaining items from list L1
while (itemL1 != null)
{
Result.add(itemL1);
if (iterL1.hasNext())
{
itemL1 = iterL1.next();
}
else
{
itemL1 = null;
}
}
//add remaining items from list L2
while (itemL2 != null)
{
Result.add(itemL2);
if (iterL2.hasNext())
{
itemL2 = iterL1.next();
}
else
{
itemL2 = null;
}
}
}
//tester class
public static void main(String[] args)
{
ArrayList<Integer> a = new ArrayList<>();
a.add(1);
a.add(3);
a.add(5);
a.add(7);
a.add(9);
a.add(12);
ArrayList<Integer> a2 = new ArrayList<>();
a2.add(1);
a2.add(2);
a2.add(3);
a2.add(9);
a2.add(20);
ArrayList<Integer> results = new ArrayList<>();
symDifference(a, a2, results);
Collections.sort(results); // sort the results
System.out.println(results.toString());
}
}
Is my code O(n)
? I am fairly certain it is, but I am not sure. I still don't fully understand time complexity. I know that if I transverse through a list then I get O(n)
, but don't know if it changes when you transverse through two lists. I do believe it is O(n)
because the lists are being transversed under one loop. Any help will be much appreciated! Thanks for your time!
Yes your code is O(m+n)
where m
is the length of the first list and n
is the length of the second list...
The complexity is same as that of merging two sorted arrays . Its just that you don't add the common numbers between the two lists in the final result..
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.