#Comparing Two ArrayLists
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
Since it's homework, I assume you are not supposed to use something like Stream.distinct(), and you have to do it the hard way. Iterate through one ArrayList and check if the value exists in the other ArrayList. If it does, add it to a HashSet of common items.
They usually want you to suffer a bit.
so you're saying create a loop that runs through the integerOne array, and then compares each element to the elements in the second?
declaration: module: java.base, package: java.util, interface: Collection
this is my first programming class so i'm still new to most of this stuff haha
Yes. That would not be the most efficient, but that is the kind of solution they usually want for homework.
no if you add all elements of both lists to a set then the set just has no duplicates but all elements of both lists and not just the elements the lists have in common
Right. I got distracted and forgot the problem.
so it would be something like for(int i:integerOne.length) {
}
then in that loop have an if statement checking if the value at position i exists in integerTwo array?
Its a bit vague that they say not to use any built in methods. Not allowed to just throw one list into a set and check nr 2 against that then, perhaps?
here are the full written instructions.
Write a program that takes two ArrayLists of integers and finds the common elements between the
two lists. The program should use a method called findCommonElements() that takes two
ArrayList<Integer> as parameters and returns a new ArrayList<Integer> containing the
common elements. You can declare and initialize two ArrayLists of Integers based on your choice.
You must not use any built-in methods to find the common elements.
i think that is what they what them to do
The instructions say you can't use built in methods, so you can't use contains(). That means you also need to iterate the other list and check each item, so you would have nested loops and not just one loop.
Sorting and merging might help you make a trivial O(n) solution 
since it is no ready made solution like retainAll
how do you sort in O(n)?
oh true im rarted
for every element A in list A
for every element B in list B
if A equals B
already_added = false
for every element COMMON in list of common elements
if A equals COMMON
already_added = true
if NOT already_added
add A to common elements
@winter hare here is a solution
its technically speaking the slowest one
but its the easiest to implement
this is the nested for-loop idea?
all ideas that loop end up being "nested for loop ideas" but yeah
cool let me give a try.
so i've got the first part with nested loop and I am now at the if statement. so if i used i for the outer loop and j for the inner loop, it would be if(i == j)?
remember that you are comparing the elements in the lists
not the indexes of those elements
people usually use i and j to track an index
hm. how would i compare the actual element then?
for (int i = 0; i < integerOne.size(); i++) {
int a = integerOne.get(i);
// ...
}
you need to use .get to get the element from the list
so that code above covers this line in the pseudocode
for every element A in list A
in the ... is where you put everything else
right.
when i go to add the element to the new array, is that done in the if statement? and what variable would i be adding? does it matter if it is a or b?
i got it! thank you all!