Question

In: Computer Science

Java Program Sequential Search You are given a sequence of n integers S and a sequence...

Java Program

Sequential Search

You are given a sequence of n integers S and a sequence of different q integers T. Write a program which outputs C, the number of integers in T which are also in the set S. Do not use any import sort packages.

Input: In the first line n is given. In the second line, n integers are given. In the third line q is given. Then, in the fourth line, q integers are given.

Output: Print C in a line.

Constraints

  • n ≤ 10000
  • q ≤ 500
  • 0 ≤ an element in S ≤ 109
  • 0 ≤ an element in T ≤ 109

Sample Input 1

5
1 2 3 4 5
3
3 4 1

Sample Output 1

3

Sample Input 2

3
3 1 2
1
5

Sample Output 2

0

Sample Input 3

5
1 1 2 2 3
2
1 2

Sample Output 3

2

Solutions

Expert Solution

PROGRAM with comments :

import java.util.Set;
import java.util.HashSet;
import java.util.Scanner;

public class Solution{
   public static void main(String args[]){
       Scanner sc = new Scanner(System.in);
      
       int n = Integer.parseInt(sc.nextLine());   //Taking input n
       String[] s = sc.nextLine().split(" ");        //Taking 2nd line input as string
      
       int q = Integer.parseInt(sc.nextLine());   //Taking input q
       String[] t = sc.nextLine().split(" ");        //Taking 4th line input as string
      
       int S[] = new int[n];   //Declaring S array of intgers to store S
       int T[] = new int[q];   //Declaring T array of intgers to store T
      
       int i=0;
       for(String ele : s){   //Loop to populate S array
           S[i++] = Integer.parseInt(ele);       //Converting string to integer and storing it to S array
       }
      
       int j=0;
       for(String ele : t){   //Loop to populate S array
           T[j++] = Integer.parseInt(ele);       //Converting string to integer and storing it to T array
       }
      
       Set<Integer> set = new HashSet<Integer>();   //Set to store result set
      
       for(int sec: T){   //Outer loop to iterate through T
           for(int first: S){   //Inner loop to iterate through S
               if(sec == first)   //checking if current element of T is equal to the element of S
                   set.add(sec);   //if equal, adding it to the set
           }
       }
      
       System.out.println(set.size());       //printing out the number of elements in the set (the result)
   }
}

SCREENSHOT :


Related Solutions

Write a program in Java which will search a space delimited string of integers for the...
Write a program in Java which will search a space delimited string of integers for the two values which add up to a provided amount. The program must output th following text to the command prompt/console: Indexes: { Index of first number} { Index of second number} Example One: NumSerach( 100, " 5 75 25"); output: Indexes: 1 2
Q1. Write a Java program to do sequential search to find element 55 in array 10,20,35,45,55,65,75,85....
Q1. Write a Java program to do sequential search to find element 55 in array 10,20,35,45,55,65,75,85.                                                                                                                                                                    Q2.Write a Java program to find element 54 in array 45,41,65,53,76,90 using Binary Search. (Hint: Binary search is applied to sorted array elements) Q4.   Write a java program to create array list subject        - add English, Maths, Science to the list        - add Computers at index 2        - display first occurrence index of Maths        - traverse the list using...
Assume you need to write a Java program that uses a binary search algorithm to search...
Assume you need to write a Java program that uses a binary search algorithm to search a sorted array for a given value. 1. Write a Java pseudocode that uses recursion to accomplish the task. Here is a hint. When you are searching for a particular value in an array, there are two possible outcomes. 1) The value is found and the array index of that value is returned. 2) The value is not found and we return -1. (5...
Problem 5 Given a table of n integers and an integer k, make a program in...
Problem 5 Given a table of n integers and an integer k, make a program in C++ that: a) Read n b) Read the table of numbers. c) Determine the number of elements in the table less than k d) Determine the number of elements in the table equal to k e) Determine the number of elements in the table greater than k f) That displays the values found
The subset-sum problem is defined as follows. Given a set of n positive integers, S =...
The subset-sum problem is defined as follows. Given a set of n positive integers, S = {a1, a2, a3, ..., an} and positive integer W, is there a subset of S whose elements sum to W? Design a dynamic program to solve the problem. (Hint: uses a 2-dimensional Boolean array X, with n rows and W+1 columns, i.e., X[i, j] = 1,1 <= i <= n, 0 <= j <= W, if and only if there is a subset of...
Write a Java program that reads a list of integers into an array. The program should...
Write a Java program that reads a list of integers into an array. The program should read this array from the file “input.txt”. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is a two-column list. The first column is the list of the distinct array elements; the second column is the number of occurrences of each element. The list should be sorted on entries in...
java Factorials The factorial of n (written n!) is the product of the integers between 1...
java Factorials The factorial of n (written n!) is the product of the integers between 1 and n. Thus 4! = 1*2*3*4 = 24. By definition, 0! = 1. Factorial is not defined for negative numbers. Write a program that asks the user for a non-negative integer and computes and prints the factorial of that integer. You’ll need a while loop to do most of the work—this is a lot like computing a sum, but it’s a product instead. And...
Stacks & Queues C++ You are given a stack of N integers such that the first...
Stacks & Queues C++ You are given a stack of N integers such that the first element represents the top of the stack and the last element represents the bottom of the stack. You need to pop at least one element from the stack. At any one moment, you can convert stack into a queue. The bottom of the stack represents the front of the queue. You cannot convert the queue back into a stack. Your task is to remove...
I. General Description In this assignment, you will create a Java program to search recursively for...
I. General Description In this assignment, you will create a Java program to search recursively for a file in a directory. • The program must take two command line parameters. First parameter is the folder to search for. The second parameter is the filename to look for, which may only be a partial name. • If incorrect number of parameters are given, your program should print an error message and show the correct format. • Your program must search recursively...
Java: create a program that reads in a piece of DNA sequence from a sequence file...
Java: create a program that reads in a piece of DNA sequence from a sequence file (dna.seq) (alternatively you can use the getRandomSeq(long) method of the RandomSeq class to generate a piece of DNA sequence), and then print out all the codons in three forward reading frames. Design a method called codon() that can be used to find all the codons from three reading frames. The method will take in an argument, the reading frame (1, 2, or 3), and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT