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

I need help creating a Java program that finds common elements in arrays. The sh

ID: 643803 • Letter: I

Question

I need help creating a Java program that finds common elements in arrays. The shortest array needs to be designated as the query collection, which is used to compare the other collections to. The algorithm used needs to run in linear time, and therefore each element in the non-query collections only participates in at most 1 comparison. This program should also contain the following:

-Create a class called CommonElements

-The CommonElements class should contain a method with the following signature:

public Comparable[] findCommonElements(Comparable[][] collections)

-The argument to this method (collections) will be a set of k collections.

-The value returned should be a collection of comparable elements that contains only the elements to all input collections

-Since this algorithm is being graded on run time, the findCommonElements method will need to maintain a running total of comparisons performed for each set of collections tested.   

-Duplicate items should be allowed

-The collections should be allowed to have varying lengths

Thank you!

Explanation / Answer

a program to find common elements between arrays

1) Using Iterative Method

In this method, we iterate both the given arrays and compare each element of one array with elements of other array. If the elements are found to be equal, we will add that element into HashSet. This method also works for those arrays which contain duplicate elements.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

class CommonElements

{

    public static void main(String[] args)

    {

        String[] s1 = {"ONE", "TWO", "THREE", "FOUR", "FIVE", "FOUR"};

        String[] s2 = {"THREE", "FOUR", "FIVE", "SIX", "SEVEN", "FOUR"};

        HashSet<String> set = new HashSet<String>();

        for (int i = 0; i < s1.length; i++)

        {

            for (int j = 0; j < s2.length; j++)

            {

                if(s1[i].equals(s2[j]))

                {

                    set.add(s1[i]);

                }

            }

        }

        System.out.println(set);     //OUTPUT : [THREE, FOUR, FIVE]

    }

}

2) Using retainAll() Method :

This is one of the easiest method to find the common elements from two arrays. In this method, we create two HashSets using given two arrays and then use reatinAll() method of HashSet to retain only common elements from the two sets.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

class CommonElements

{

    public static void main(String[] args)

    {

        Integer[] i1 = {1, 2, 3, 4, 5, 4};

        Integer[] i2 = {3, 4, 5, 6, 7, 4};

        HashSet<Integer> set1 = new HashSet<>(Arrays.asList(i1));

        HashSet<Integer> set2 = new HashSet<>(Arrays.asList(i2));

        set1.retainAll(set2);

        System.out.println(set1);     //Output : [3, 4, 5]

    }

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

class CommonElements

{

    public static void main(String[] args)

    {

        String[] s1 = {"ONE", "TWO", "THREE", "FOUR", "FIVE", "FOUR"};

        String[] s2 = {"THREE", "FOUR", "FIVE", "SIX", "SEVEN", "FOUR"};

        HashSet<String> set = new HashSet<String>();

        for (int i = 0; i < s1.length; i++)

        {

            for (int j = 0; j < s2.length; j++)

            {

                if(s1[i].equals(s2[j]))

                {

                    set.add(s1[i]);

                }

            }

        }

        System.out.println(set);     //OUTPUT : [THREE, FOUR, FIVE]

    }

}

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