Question

consider two sets of integer values stored as arrays x and y of size m and...

consider two sets of integer values stored as arrays x and y of size m and n respectively. For example, x[5]={2,68,10,90,4} and y[4]={68,3,10,22}. Write a program to print the intersection (intersection returns the elements that are common in both sets) of these two sets, i.e., the set {68, 10}.

In JAVA Programming language

Homework Answers

Answer #1

Program code to copy:-

import java.util.Scanner;
public class Intersection {
   public static void main(String[] args) {
      
       //Create scanner object for standard input
       Scanner scan = new Scanner(System.in);
  
       //Prompt & read the size of array from user
       System.out.print("Enter the size of first array: ");
int m = scan.nextInt();
//Declare array of m size
int x[] = new int[m];
//Prompt & read m elements for first array
System.out.print("Enter the elements for first array: ");
for(int i = 0;i < m;i++)
   x[i] = scan.nextInt();
  
//Prompt & read the size of array from user
System.out.print("Enter the size of second array: ");
int n = scan.nextInt();
//Declare array of n size
int y[] = new int[n];
//Prompt & read n elements for second array
System.out.print("Enter the elements for second array: ");
for(int i = 0;i < n;i++)
   y[i] = scan.nextInt();
  
  
System.out.print("The intersection of two sets: ");
int found=0;
//Loop to find the intersection of two arrays
for(int i=0;i < m;i++)
{
for(int j=0;j < n;j++)
{
if(x[i]==y[j])
{
   found=1;
}
}
if(found==1)
   System.out.print(x[i]+" ");
  
found=0;
}
   }

}

Screenshot of output:-

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
C++ Write a program that takes two integer arrays of different sizes from the user (this...
C++ Write a program that takes two integer arrays of different sizes from the user (this means that the user can tell the program the size of each array), and then computes the intersection and union of those arrays. Note: the arrays can be of different lengths and the intersection/union should have unique elements in sorted order. Use pointer notation of arrays for this question. Eg: *(arr+1) [10]
Write spim program and execute it on mars. Your program reads two integer values x and...
Write spim program and execute it on mars. Your program reads two integer values x and y. Write a function called sum that gets x and y passed as parameters and return the sum of odd values between x and y inclusive. In addition write another function called even that gets x and y as parameters and returns the count of all even numbers between x and y inclusive. Also in main print the quotient and remainder of diving y...
Write a C program to combine two arrays of the same size arranged in order descendant....
Write a C program to combine two arrays of the same size arranged in order descendant. Test data : Enter the number of elements to be stored in the first array: 3 Input 3 elements in the arrangement: element [0]: 1 element [1]: 2 element [2]: 3 Enter the number of elements to be stored in the second array: 3 Input 3 elements in the arrangement: element [0]: 1 element [1]: 2 element [2]: 3 Expected output: The combined array...
Q3) Write a function that takes two arrays and their size as inputs, and calculates their...
Q3) Write a function that takes two arrays and their size as inputs, and calculates their inner product (note that both arrays must have the same size so only one argument is needed to specify their size). The inner product of two arrays A and B with N elements is a scalar value c defined as follows: N−1 c=A·B= A(i)B(i)=A(0)B(0)+A(1)B(1)+···+A(N−1)B(N−1), i=0 where A(i) and B(i) are the ith elements of arrays A and B, respectively. For example, theinnerproductofA=(1,2)andB=(3,3)isc1 =9;andtheinnerproductofC= (2,5,4,−2,1)...
Write a Hack Assembly Language program to calculate the quotient from a division operation. The values...
Write a Hack Assembly Language program to calculate the quotient from a division operation. The values of dividend a and divisor b are stored in RAM[0] (R0) and RAM[1] (R1), respectively. The dividend a is a non-negative integer, and the divisor b is a positive integer. Store the quotient in RAM[2] (R2). Ignore the remainder. Example: if you are given two numbers 15 and 4 as dividend a (R0) and divisor b (R1), respectively, the answer will be 3 stored...
Write an application that prompts a user for two integers and displays every integer between them....
Write an application that prompts a user for two integers and displays every integer between them. Display There are no integers between X and Y if there are no integers between the entered values. Make sure the program works regardless of which entered value is larger. This is a java assignment. Pls help
Create a function mult_arr which accepts two pointers-to-double as input along with an integer reflecting the...
Create a function mult_arr which accepts two pointers-to-double as input along with an integer reflecting the length of each array. The function should return void. Using pointer arithmetic to access the array elements, compute the element-wise product of the two arrays in-place (the result is stored in the first array). The function should return void. In main, read in two arrays of 10 values from the user following the I/O format seen in the example run below. On a new...
Function Example: Write a Python function that receives two integer arguments and writes out their sum...
Function Example: Write a Python function that receives two integer arguments and writes out their sum and their product. Assume no global variables. def writer(n1, n2): sum = n1 + n2 product = n1*n2 print("For the numbers", n1, "and", n2) print("the sum is", sum) print("and the product is", product) ... 1) Create a PYHW2 document that will contain your algorithms in flowchart and pseudocode form along with your screen shots of the running program. 2) Create the algorithm in both...
float sum( float x, float y, float z ) ​program in c++
Here are the function prototypes for your homeworkWrite out the actual functions themselves Also, create a main function that demonstrates that all four functions work The user should be able to provide four values to your program (three floats and an int) Your program should then use your four new functions and also display the results to the user NONE of these functions should print values themselvesfloat sum( float x, float y, float z ); // returns the sum of three floatsfloat mean( float...
JAVA please Arrays are a very powerful data structure with which you must become very familiar....
JAVA please Arrays are a very powerful data structure with which you must become very familiar. Arrays hold more than one object. The objects must be of the same type. If the array is an integer array then all the objects in the array must be integers. The object in the array is associated with an integer index which can be used to locate the object. The first object of the array has index 0. There are many problems where...