#Comparing Two ArrayLists

1 messages · Page 1 of 1 (latest)

winter hare
#

I have an assignment where our professor wants us to create two different integer arraylists and then find common terms using another method named findCommonElements. Here is what I have as of now. I'm not sure where to go from here.

midnight ridgeBOT
#

<@&987246399047479336> please have a look, thanks.

old drift
#

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.

winter hare
#

so you're saying create a loop that runs through the integerOne array, and then compares each element to the elements in the second?

winter hare
#

this is my first programming class so i'm still new to most of this stuff haha

old drift
proud kelp
#

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

old drift
winter hare
#

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?

hearty pendant
#

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?

winter hare
#

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.

proud kelp
old drift
hearty pendant
#

Sorting and merging might help you make a trivial O(n) solution peepo_happy

proud kelp
hearty pendant
#

oh true im rarted

west cradle
#
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

winter hare
west cradle
winter hare
#

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)?

west cradle
#

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

winter hare
#

hm. how would i compare the actual element then?

west cradle
#
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

winter hare
#

ah ive seen that get method before.

#

ok

west cradle
#

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

winter hare
#

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?

west cradle
#

if a and b are equal

#

then no

winter hare
#

i got it! thank you all!