Question

In: Computer Science

Please use Java language to write two efficient functions: Write an efficient function that compute the...

Please use Java language to write two efficient functions:

  1. Write an efficient function that compute the intersections of two sorted arrays of integers

  2. Write an efficient function that compute the intersection of two arrays of integers (not

    necessary sorted).

Solutions

Expert Solution

CODE

import java.util.HashSet;

public class Main
{
   public static void findIntersectionForSortedArrays(int arr1[], int arr2[])
   {
       int m = arr1.length;
       int n = arr2.length;
       int i = 0, j = 0;
       while (i < m && j < n)
       {
           if (arr1[i] < arr2[j])
               i++;
           else if (arr2[j] < arr1[i])
               j++;
           else
           {
               System.out.print(arr2[j++]+" ");
               i++;
           }
       }
   }
  
   public static void findIntersectionForUnsortedArrays(int arr1[], int arr2[])
    {
        HashSet<Integer> hs = new HashSet<>();
        
        for (int i = 0; i < arr1.length; i++)
            hs.add(arr1[i]);
        
        for (int i = 0; i < arr2.length; i++)
            if (hs.contains(arr2[i]))
               System.out.print(arr2[i] + " ");
    }

   public static void main(String args[])
   {
       int arr1[] = {1, 3, 4, 6, 8, 10, 11};
       int arr2[] = {3, 4, 7, 8};
       findIntersectionForSortedArrays(arr1, arr2);
      
       System.out.println();
       int arr3[] = {1, 9, 5, 4, 8, 6};
        int arr4[] = {7, 8, 9, 1, 17, 20, 4};
        findIntersectionForUnsortedArrays(arr3, arr4);
   }
}


Related Solutions

Please use Java language to write an efficient function that compute the intersection of two arrays...
Please use Java language to write an efficient function that compute the intersection of two arrays of integers (not necessary sorted).
Write this in java please. I use Eclipse Write the following two functions. main doesnt need...
Write this in java please. I use Eclipse Write the following two functions. main doesnt need to be filled up. Function 1: Write a function called howMany that takes two arguments: an array of integers called arr, and an integer called iTarget. The function should return an integer result. Inside the function write a loop to go through the array and count the number of elements in the array that have the same value as iTarget. At the end it...
Please use the Java Programming language. This is the introductory course, chapter two. Please only use...
Please use the Java Programming language. This is the introductory course, chapter two. Please only use if/else if, else and while loop. We have not touch base with do and while do(I don't know if while do exist in Java). Create an application that converts number grades to letter grades. Console Welcome to the Letter Grade Converter Enter numerical grade: 90 Letter grade: A Continue? (y/n): y Enter numerical grade: 88 Letter grade: A Continue? (y/n): y Enter numerical grade:...
Please use Java language! with as much as comment! thanks! Write a program that displays a...
Please use Java language! with as much as comment! thanks! Write a program that displays a frame with a three labels and three textfields. The labels should be "width:", "height:", and "title:" and should each be followed by one textfield. The texfields should be initialized with default values (Example 400, 600, default title), but should be edited by the user. There should be a button (label it whatever you want, I don't care). If you click the button, a new...
Please use Java language! with as much as comment! thanks! Write a program that displays a...
Please use Java language! with as much as comment! thanks! Write a program that displays a frame with a three labels and three textfields. The labels should be "width:", "height:", and "title:" and should each be followed by one textfield. The texfields should be initialized with default values (Example 400, 600, default title), but should be edited by the user. There should be a button (label it whatever you want, I don't care). If you click the button, a new...
How to use a Java program that calls for two functions. The first function is asking...
How to use a Java program that calls for two functions. The first function is asking the user for a two integer numbers, then display the sum of the numbers. The second method is asking the user for two floating point numbers, then display the product of the numbers. Call these methods addNumbers and multiplyNumbers respectively. Call the program AddAndMultiply
//Using Java language Write a copy instructor for the following class. Be as efficient as possible....
//Using Java language Write a copy instructor for the following class. Be as efficient as possible. import java.util.Random; class Saw {    private int x;    private Integer p; //------------------------------------------ public Saw() { Random r = new Random();        x = r.nextInt();        p = new Integer(r.nextInt()); } }
java In one program, write 3 separate functions, and use them. 1) Write the function body...
java In one program, write 3 separate functions, and use them. 1) Write the function body for each: int squareInteger s( int x) double squareDouble( double d) float squareFloat ( float f) They basically do the same thing, square a number, but use different argument datatypes. 2) Test Each function with: Using int 4, double 4.9, float 9.4 What happened when you use the correct numerical data type as input ? What happened when you use the incorrect numerical data...
Even Odd Average (C++ LANGUAGE) Write the following functions: Function #1 Write the function Print. The...
Even Odd Average (C++ LANGUAGE) Write the following functions: Function #1 Write the function Print. The function will have one int 1D array n and one int size as parameters. The function will display all the values of n on one line. Function #2 Write the function AverageEvenOdd. The function will have one int 1D array n and one int size as parameters. The size of the array is given by the parameter int size. Therefore, the function should work...
Please use Java language with comments! Thanks! Write a program that will display multiple dots move...
Please use Java language with comments! Thanks! Write a program that will display multiple dots move across the Frame and randomly change direction when they hit the edge of the screen. Do this by creating a Dot class and making each dot an object of that class. You may reuse code written in class for this part of the assignment. Create a subclass of the Dot class called PlayerDot that is controlled by the player through keyboard input (arrow keys)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT