I am looking for some clarification on this solution. Can anyone guide me on the following two points:
- Is the below algorithm good solution?
- and is my Big O calculation correct?
your clarification is highly appreciated. Thanks in advance
public static void main(String[] args) {
String[] a = {"a", "b", "c", "d"};
String[] b = {"z", "f", "c"};
boolean value1 = find(a, b);
System.out.println(value1);
boolean value2 = findArray(a, b);
System.out.println(value2);
}
/*
since the both the array is of diff size for nested loop
Big O = O(n*n)
if array size is same Big O = O(n^2)
*/
private static boolean find(String[] a, String[] b) {
for (int i = 0; i < a.length; i++) {
String val1 = a[i];
for (int j = 0; j < b.length; j++) {
if (val1.equals(b[j])) {
System.out.println(val1 + " : " + b[j]);
return true;
}
}// O(n)
}// O(n)
return false;
}// O(n*n)
/*
Big O = O(n)
*/
private static boolean findArray(String[] a, String[] b) {
//create array list from array
List<String> list = new ArrayList<>(Arrays.asList(b));
for (int i = 0; i < a.length; i++) {
String val1 = a[i]; //O(1)
if (list.contains(val1)) {
System.out.println(val1 + " : contain in list b");
return true;
}// O(1)
}// O(n)
return false;
}// O(n)