Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Given two lists of integers, write a function that returns a list that contains

ID: 3541303 • Letter: G

Question

Given two lists of integers, write a function that returns a list that contains only the intersection (elements that occur in both lists) of the two lists. The returned list should only contain unique integers, no duplicates.

For example, [4, 2, 73, 11, -5] and [-5, 73, -1, 9, 9, 4, 7] would return the list [-5, 4, 73] in no particular order.

You may use the JDK or the standard template library. The solution will be evaluated on correctness, runtime complexity (big-O), and adherence to coding best practices. A complete answer will include the following:

Explanation / Answer

Complete documented code in java



import java.util.*;

public class DuplicateElements {

public static void main(String[] args) {

//Making first List

List<Integer> firstList = new ArrayList<Integer>();

// Making second list

List<Integer> secondList = new ArrayList<Integer>();

// Making third list to store all duplicate values

List<Integer> finalList = new ArrayList<Integer>();

// adding contents to the first list. You can add as many you want.

firstList.add(-5);

firstList.add(73);

firstList.add(-1);

firstList.add(9);

firstList.add(9);

firstList.add(4);

firstList.add(7);

// adding contents to the second list. You can add as many you want.

secondList.add(4);

secondList.add(2);

secondList.add(73);

secondList.add(11);

secondList.add(-5);

// checking if the element in first list is also available in second list.

for (int i = 0; i < firstList.size(); i++){

if( secondList.contains(firstList.get(i))){

// if the integer is already added in the final list, skip

if (finalList.contains(firstList.get(i))){

}

// else add the integer to final list

else

finalList.add(firstList.get(i));

}

}

// print the contents of the final list which contains all duplicate items

for (int j = 0; j < finalList.size(); j++){

System.out.println(finalList.get(j));

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote