The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Jul 2017 |
| Last Sign in: | 313 Weeks Ago, 5 Days Ago |
| Questions Answered: | 15833 |
| Tutorials Posted: | 15827 |
MBA,PHD, Juris Doctor
Strayer,Devery,Harvard University
Mar-1995 - Mar-2002
Manager Planning
WalMart
Mar-2001 - Feb-2009
I need to use a HashSet that has a linear running time to find the elements that are common in all lists. For example, the following collection of lists of integers:
A1=[Â 3Â 4Â 9Â 8Â 12Â 15 7 13]
A2=[ 15 24 50Â 12 3 9 3Â ]
A3=[ 78Â 9Â 65 24 13Â 9 3 12]
A4=[ 15 78 14Â 3Â 2Â 9Â 44Â 12Â ]
Then the common elements in this collection would be: [Â 3 9 12]Â
Â
With the class CommonElements, I need to encapsulate the algorithm within a method called findCommonElements, that has the following signature:
The input argument to this method is a nested list (i.e., a list of list). The value returned by your method is a list of common elements. The algorithm for this method must use a HashSet and should have an average running time of O(nk) where n is the maximum number of elements in each list and K is the number of lists in the collection.
Attention: May NOT use the retainAll method in java.utilHashSet.Â
Â
This is the main method:
public static void main(String[] args)
{
List<List<Integer>>collections =new ArrayList<List<Integer>>(); collections.add(0,
new ArrayList<Integer>(Arrays.asList(3, 4, 9, 8, 12, 15, 7, 13)));
collections.add(1,
new ArrayList<Integer>(Arrays.asList(15,24,50,12,3,9)));
collections.add(2,
new ArrayList<Integer>(Arrays.asList(78,65,24,13,9,3,12)));
collections.add(3, new ArrayList<Integer>(Arrays.asList(15,78,14,3,2,9,44,12)));
List<Integer> result =findCommonElements(collections); }Â
Â
Â
I need the following:
----------- Â ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly