Question

In: Computer Science

I want to print out the array as well as its intersection, and also it doesn't...

I want to print out the array as well as its intersection, and also it doesn't show the time elapsed after 100,000

Missing is: 1,000,000 and 10,000,000

import java.util.Random;
import java.util.HashSet;
import java.util.Arrays;

public class array_intersect {

public static void main(String[] args)
{
for(int i=1000; i <= 10000000; i= i*10)
{
Integer[] array1 = generate_random_array(i);
Integer[] array2 = generate_random_array(i);

long startTime = System.nanoTime();
get_intersection(array1, array2);
long stopTime = System.nanoTime();
long elapsedTime = stopTime - startTime;
System.out.println("Time elapsed for dataset of " + i + " points : "+ elapsedTime + " in nanoseconds");

}
}

public static Integer[] generate_random_array(int size)
{
Random rand = new Random();
Integer[] return_array = new Integer[size];
for(int i=0;i< size ; i ++)
{
return_array[i] = rand.nextInt(size);
}
return return_array;
}

public static Integer[] get_intersection(Integer[] array1, Integer[] array2)
{
HashSet<Integer> set = new HashSet<>();
set.addAll(Arrays.asList(array1));
set.retainAll(Arrays.asList(array2));

Integer[] intersection = {};
intersection = set.toArray(intersection);

return intersection;

}


}

Solutions

Expert Solution

Its is printing after 100,000 it just takes more time.

As you can see from the above picture, it takes 939930274600 nanoseconds( ~15 mins) for printing 1000,000 data sets. So if you wait it will print all the results.

For printing, the arrays and insection will mess up your console. I will suggest you avoid it but still, I have provided the code(its highlighted) for that.

Program

import java.util.Random;
import java.util.HashSet;
import java.util.Arrays;

public class Test {

   public static void main(String[] args) {
       for (int i = 1000; i <= 10000000; i = i * 10) {
           Integer[] array1 = generate_random_array(i);
           Integer[] array2 = generate_random_array(i);

           long startTime = System.nanoTime();
           Integer[] intersection=get_intersection(array1, array2);
           long stopTime = System.nanoTime();
           long elapsedTime = stopTime - startTime;
          
          
           System.out.println("Array1: "+Arrays.toString(array1));
           System.out.println("Array2: "+Arrays.toString(array2));
           System.out.println("Intersection: "+Arrays.toString(intersection));

          
           System.out.println("Time elapsed for dataset of " + i + " points : " + elapsedTime + " in nanoseconds");

       }
   }

   public static Integer[] generate_random_array(int size) {
       Random rand = new Random();
       Integer[] return_array = new Integer[size];
       for (int i = 0; i < size; i++) {
           return_array[i] = rand.nextInt(size);
       }
       return return_array;
   }

   public static Integer[] get_intersection(Integer[] array1, Integer[] array2) {
       HashSet<Integer> set = new HashSet<>();
       set.addAll(Arrays.asList(array1));
       set.retainAll(Arrays.asList(array2));

       Integer[] intersection = {};
       intersection = set.toArray(intersection);

       return intersection;

   }

}

If you have any further doubts feel free to comment


Related Solutions

The following code was meant to print out the elements in an array in reverse order....
The following code was meant to print out the elements in an array in reverse order. However, it does not behave correctly. public static void reverse(int[] a, int index) {       if (index == (a.length - 1))         System.out.printf("%d%n", a[index]);       else {         reverse(a, index); What does it do? Explain why it behaves in this way and There is more than one error in the code. Correct the code so that it will recursively print out the elements of...
In C Create a multi-dimensional array and print it out forwards, backwards and then transpose.
In C Create a multi-dimensional array and print it out forwards, backwards and then transpose.
Please explain your answer thoroughly because I want to understand it well and also please include...
Please explain your answer thoroughly because I want to understand it well and also please include a diagram if possible Two objects which have mass: m1 = 10kg and m2 = 20kg. Both of them are moving at the velocity of: v1 = 20^i ms and v2 = 10^j ms and then the two objects collide completely inelastically. In what direction do the two objects go after the collision? After the collision, how much kinetic energy was lost?
This is what I have so far. The out print I am looking for is Date...
This is what I have so far. The out print I am looking for is Date is 7/22/2020 New month: 9 New day: 5 New Year: 2020 Date is 9/5/2020 #include    class Date {    public:        Date::Date(int dateMonth, int dateDay, int dateYear)        month{dateMonth}, day{dateDay}, int dateYear}{}               //void displayDate();         //set month        void Date::setMonth(std::int dateMonth){            month = dateMonth;        }        //retrieve month   ...
The following code was meant to print out the elements in an array in reverse order. However, it does not behave correctly.
  The following code was meant to print out the elements in an array in reverse order. However, it does not behave correctly. public static void reverse(int[] a, int index) {       if (index == (a.length - 1))         System.out.printf("%d%n", a[index]);       else {          reverse(a, index); What does it do? Explain why it behaves in this way.                                    There is more than one error in the code. Correct the code so that it will recursively print out...
i want a program in java that finds the shortest path in a 2D array with...
i want a program in java that finds the shortest path in a 2D array with obstacles from source to destination using BFS and recursion. The path must be stored in a queue. The possible moves are left,right,up and down.
anyone can explain why this code in R doesn't work? i also tried to use filter...
anyone can explain why this code in R doesn't work? i also tried to use filter function, no hope either! b <- subset(sub,NEIGHBORHOOD == "HARLEM-CENTRAL") the name of the variable is correct, the condition. is correct too.
Can you fix to me this code plz I just want to print this method as...
Can you fix to me this code plz I just want to print this method as the reverse. the problem is not printing reverse. public void printBackward() {               Node curr = head;        Node prev = null;        Node next = null;               System.out.print("\nthe backward of the linkedlist is: ");               while(curr != null) {            next = curr.next;            curr.next = prev;   ...
How can I write a simple MIPS program to print out the following elements of an...
How can I write a simple MIPS program to print out the following elements of an array of the size of 10: 5,10,15,20,25,30,35,40,45,50
I need original java code that completes this program and gets it to print out results...
I need original java code that completes this program and gets it to print out results just like in the example. Also please upload answer in a word document format only. Implement both linear search and binary search, and see which one performs better given an array 1,000 randomly generated whole numbers (between 0-999), a number picked to search that array at random, and conducting these tests 20 times. Each time the search is conducted the number of checks (IE...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT