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).
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...
//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)...
java. please don't use complicated language I can follow up. Write a for loop that prints...
java. please don't use complicated language I can follow up. Write a for loop that prints the integers from 1 to 100, all on one line, space-separated. However, after printing 3 numbers, you need to skip the next number and print a counter in parenthesis. 1 2 3 (1) 5 6 7 (2) 9 10 11 (3) 13 14 15 (4) 17 18 19 [... and so on ...] Write this code once with for loop, once with while loop,...
Language: Python 3 (Please structure answer as basic as possible) Write a function that involves two...
Language: Python 3 (Please structure answer as basic as possible) Write a function that involves two arguments, named changeTheCase(myFile, case), that takes, as arguments, the name of a file, myFile, and the case, which will either be “upper” or “lower”. If case is equal to “upper” the function will open the file, convert all characters on each line to upper case, write each line to a new file, named “upperCase.txt”, and return the string “Converted file to upper case.” If...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT