Find an array inside another larger array
The requirement of "using just core Java API's" could also mean that they wanted to see whether you would reinvent the wheel. So in addition to your own implementation, you could give the one-line solution, just to be safe:
public static int findArray(Integer[] array, Integer[] subArray)
{
return Collections.indexOfSubList(Arrays.asList(array), Arrays.asList(subArray));
}
It may or may not be a good idea to point out that the example given contains invalid array literals.
Find an Array inside another larger Array in Java
This Code will solve your problem:
use a variable temp where it will be the tracking index of first array and moves forward in every check.
public static boolean contains(int[] a, int[] b) {
int i=0;
int j=0;
boolean result = false;
if(a.length == 0){
System.out.println(false);
return false;
}else if(b.length ==0){
System.out.println(true);
return true;}
int count = 0;
int temp = 0;
for (i=0;i<b.length;i++){
for (j= temp;j<a.length;j++){
if (b[i] == a[j]){
count = count+1;
temp++;
break;
}
temp++;
}
}
if (count == b.length){
result = true;
} else {
result =false;
}
System.out.println(result);
return result;
}
JavaScript - find index of array inside another array
To compare the actual values you can use the JSON.stringify() method:
piece = [5, 10];
array = [[5, 10], [5, 11]];
//Using findIndex
console.log(array.findIndex(function(element) {
return JSON.stringify(element) == JSON.stringify(piece);
}));
Find Object inside Array inside another Array
You can first flaten this array to an array of inner objects and then use filter()
or find()
.
let bigArray = [ { Name: 'Trump', children: [ {Name: 'TrumpChild1', City: 'city1'}, {Name: 'TrumpChild2', City: 'city2'} ] }, { Name: 'Barack Obama', children: [ {Name: 'Barack Obama Child1', City: 'city3'}, {Name: 'Barack Obama Child2', City: 'city4'} ] }, { Name: 'Clinton', children: [ {Name: 'Clinton Child 1', City: 'city5'}, {Name: 'Clinton Child2', City: 'city6'} ] }];
let all = bigArray.reduce((prev, next) => prev.concat(next.children), []);let result = all.find(obj => obj.City === 'city1');console.log(result);
// if there can be multiple matches then use filterlet results = all.filter(obj => obj.City === 'city1');console.log(results);
What is the fastest way to find an array within another array in Java?
Regardless of the elements of your arrays, I believe this is not much different than the string search problem.
This article provides a general intro to the various known algorithms.
Rabin-Karp and KMP might be your best options.
You should be able to find Java implementations of these algorithms and adapt them to your problem.
Array Subset of Another Array
You can use map()
with indexOf()
:
const part = ["So 2545", "Cool 1123", "Mos 1999"];
const full = ["So 2545", "Fun 1023", "Loss 2009", "Cool 1123", "Mos 1999"];
const indexes = part.map(v => full.indexOf(v));
console.log(indexes);
Fastest way to find a 2d array inside another array that holds multiple 2d arrays
You can use NumPy.all
with a two-axis then use NumPy.argwhere
for finding the index like below:
b = np.array ([[0, 0, 0], [0, 0, 0], [1, 1, 1]])
a = np.array([[[1, 1, 1], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [1, 1, 1], [0, 0, 0]],[[0, 0, 0], [0, 0, 0], [1, 1, 1]],[[0, 0, 1], [0, 0, 1], [0, 0, 1]],[[0, 1, 0], [0, 1, 0], [0, 1, 0]],[[1, 0, 0], [1, 0, 0], [1, 0, 0]]])
np.all(b == a, axis=(-1))
# array([[False, True, False],
# [ True, False, False],
# [ True, True, True],
# [False, False, False],
# [False, False, False],
# [False, False, False]])
np.all(b == a, axis=(-1,1))
# array([False, False, True, False, False, False])
indexs = np.argwhere(np.all(b == a, axis=(-1, 1)))
Output:
>>> indexs
array([[2]])
Search array within another array - Algorithm
As already mentioned here :
https://stackoverflow.com/a/3940684/351861:
public static int findArray(Integer[] array, Integer[] subArray)
{
return Collections.indexOfSubList(Arrays.asList(array), Arrays.asList(subArray));
}
Java has builting features for that, apparently.
Finding if an array contains all elements in another array
You could check that the larger of the arrays outer
contains every element in the smaller one, i.e. inner
:
public static boolean linearIn(Integer[] outer, Integer[] inner) {
return Arrays.asList(outer).containsAll(Arrays.asList(inner));
}
Note: Integer
types are required for this approach to work. If primitives are used, then Arrays.asList
will return a List
containing a single element of type int[]
. In that case, invoking containsAll
will not check the actual content of the arrays but rather compare the primitive int
array Object
references.
Related Topics
Having a Column Name as Input Parameter of a Preparedstatement
Differencebetween <Jsp:Include Page = ... > and <%@ Include File = ... >
How to Loop User Input Until an Integer Is Inputted
Abnormal Behaviour of the Zoom in and Zoom Out Functionality of the Jfreechart
What Version of Javac Built My Jar
Java's Fork/Join VS Executorservice - When to Use Which
How to Add Days to a Date in Java
What Does Inputstream.Available() Do in Java
Java Hashmap Performance Optimization/Alternative
What Is the Most Accurate Encoding Detector
Building an Uberjar with Gradle
Display Mouse Coordinates Near Mouse as Hints on Mouse Move
How to Use Classloader.Getresources() Correctly
What Is the Best Library for Java to Grid/Cluster-Enable Your Application
Using Two Values for One Switch Case Statement