In: Computer Science
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.
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.