Question

In: Computer Science

How can I check to see if 2 arrays contain the same element using nothing worse...

How can I check to see if 2 arrays contain the same element using nothing worse than linear runtime? (Java)

For example if I have 2 arrays with elements {7, 8, 5, 4, 3} and {10, 12, 15, 20, 8}

I would want it to return true since there is an 8 in each array.

Solutions

Expert Solution

Hey,

Note: Brother in case of any queries, just comment in box I would be very happy to assist all your queries.

import java.util.*;
public class TEST
{
public static boolean check(int[] a,int[] b)
{
       Hashtable tab = new Hashtable();

       for(int i=0;i<a.length;i++)
       {
       String s=String.format("t%d",i);

       tab.put(s, new Integer(a[i]));
       }
       for(int i=0;i<b.length;i++)
       {

          if (tab.contains(b[i]))
          {
          return true;
          }
       }
       return false;
  

}
public static void main(String []args){
int[] a={7, 8, 5, 4, 3};
int[] b={10, 12, 15, 20, 8};
System.out.println(check(a,b));
}

}

Kindly revert for any queries

Thanks.


Related Solutions

I need to find the kth smallest element in the union of 2 sorted arrays in...
I need to find the kth smallest element in the union of 2 sorted arrays in O(log |A| + log |B|) time. I understand this is similar to binary search, but I don't understand which parts of the arrays I can throw out at each level of recursion, and why. In the pseudocode below, what should go into $1 through $16 and why? // A and B are each sorted into ascending order, and 0 <= k < |A|+|B| //...
Can someone check to see if I answered them correctly? The answers are highlighted in Bold...
Can someone check to see if I answered them correctly? The answers are highlighted in Bold font. I also need help with Question 6 1) What criteria must sales transactions meet in order for the seller to recognize revenues before collecting cash? a. The revenues must be earned (the firm must have achieved substantial performance). b. The amount to be received must qualify as an asset (there must be a future economic benefit and the amount must be measured with...
Working on a program but nothing is being sent to the output file. How can I...
Working on a program but nothing is being sent to the output file. How can I fix this? import java.util.Scanner; import java.io.*; public class Topic7Hw {    static Scanner sc = new Scanner (System.in);       public static void main(String[] args) throws IOException {               PrintWriter outputFile = new PrintWriter ("output.txt");               outputFile.println("hello");               final int MAX_NUM = 10;    int[] acctNum = new int[MAX_NUM];    double[] balance = new double[MAX_NUM];...
When using scapy in python how can I get the same result as the snippit bellow?...
When using scapy in python how can I get the same result as the snippit bellow? (which in run in terminal) >>> sr(IP(dst="192.168.8.1")/TCP(dport=[21,22,23])) Received 6 packets, got 3 answers, remaining 0 packets (<Results: UDP:0 TCP:3 ICMP:0 Other:0>, <Unanswered: UDP:0 TCP:0 ICMP:0 Other:0>) >>> ans,unans=_ >>> ans.summary() IP / TCP 192.168.8.14:20 > 192.168.8.1:21 S ==> Ether / IP / TCP 192.168.8.1:21 > 192.168.8.14:20 RA / Padding IP / TCP 192.168.8.14:20 > 192.168.8.1:22 S ==> Ether / IP / TCP 192.168.8.1:22 >...
Working with MATLAB arrays. You can check your answers on MATLAB but do not include a...
Working with MATLAB arrays. You can check your answers on MATLAB but do not include a script file or MATLAB output for this problem . Just write the commands you would use each part. You may not 'hard code' which means you cannot just write in the answers. Show the MATLAB commands You have the following array: Data1[ 3.6 8.9 2.5 4.6 12.0 3.3 7.8 1.5] A) Write the command(s) you would use to order the values in the array...
Describe how stacks and their main operations can be implemented using arrays and stacks. Provide the...
Describe how stacks and their main operations can be implemented using arrays and stacks. Provide the pros and cons for each approach. Research some applications, that can use stacks as a possible implementation vehicle.
How can I check that if a string only make from specific character? For example, I...
How can I check that if a string only make from specific character? For example, I want to check if a string only make from ICDM characters. It pass the test if it makes from ICMD. It fail the test if it makes from any special character like @#$& or lower case. Example inputs: if string = CIM, it passed if string = AIM, it failed if string = MCDI, it passed if string = ICDF, it failed This can...
First assignment for C++. How do I setup this dynamic multiplication table using 2D arrays and...
First assignment for C++. How do I setup this dynamic multiplication table using 2D arrays and double pointers? The assignment asks to Write a program that displays a 2D multiplication table based on row and column value specified by the user. Perform a data validation to ensure the number of rows and columns given by the user exist on the interval [1, 10]. If possible, protect against inputs that contain symbols that would normally crash the program (i.e. letter, symbols,...
How can I see that the the luminosity of the sun is 3.828×1026 W by looking...
How can I see that the the luminosity of the sun is 3.828×1026 W by looking at the HR diagram?
How can I make a method to check out a customer given their name and booking...
How can I make a method to check out a customer given their name and booking number in Java. I have provided the Book class as well as the customer class. I have done the method to add a customer into the array however im failng to implement a method that checks out the customer. If you need more classes associated with the system please let me know ASAP and I will provide them, ----------------------------------------------------------------------------------------------------------------------------------- public class Book{    String...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT