Reputation: 47
I need to create an array with values which appear in both of the given arrays.
I was thinking about looping through each of the arrays and comparing values, if they match increase a 'counter' variable which will be the length of the new array, loop through the new array and assign the values to array elements.
I need to figure out a solution with a loop, code below is what I've got so far
class New {
public static void main(String[] args) {
int arr1[] = {2, 4, 5, 7, 9, 10};
int arr2[] = {1, 2, 5, 6, 8};
int counter = 0;
int combined[] = new int[counter];
for (int s = 0; s < arr1.length; s++) {
for (int x = 0; x < arr2.length; x++) {
for (int i = 0; i < combined.length; i++) {
if (arr1[s] == arr2[x]) {
counter++;
combined[i] = arr1[s];
}
}
}
for (int i = 0; i < combined.length; i++) {
System.out.print(combined[i] + " ");
}
}
}
}
Upvotes: 1
Views: 122
Reputation: 513
In Java 8 you can try:
public static void main(String[] args) {
Integer[] arr1 = {2, 4, 5, 7, 9, 10};
Integer[] arr2 = {1, 2, 5, 6, 8};
List<Integer> integers = Stream.of(arr1)
.filter(Arrays.asList(arr2)::contains)
.collect(Collectors.toList());
System.out.println(integers);
}
Output will be:
[2, 5]
Upvotes: 0
Reputation: 15423
This will do the trick :
public static void main(String[] args) {
int arr1[] = { 2, 4, 5, 7, 9, 10 };
int arr2[] = { 1, 2, 5, 4, 8 };
int combined[] = new int[arr1.length];
for (int x = 0; x < arr1.length; x++) {
for (int i = 0; i < arr2.length; i++) {
if (arr1[x] == arr2[i]) {
combined[x] = arr1[x];
}
}
}
for (int i = 0; i < combined.length; i++) {
System.out.print(combined[i] + " ");
}
}
combined
is defined to have the same size of the larger array (to avoid any ArrayOutOfBoundsException
)combined
will hold only the
duplicate entries along with some zeroes as it was initialized using the size of arr1
. (As an exercise you can alter the loop to filter out the non-zero elements indicating the duplicates only!)Upvotes: 0
Reputation: 23800
Here you go:
int[] intersection = IntStream.of(arr1)
.filter(v1 -> IntStream.of(arr2).anyMatch(v2 -> v2 == v1))
.toArray();
Upvotes: 0
Reputation: 51945
The first problem is that you initialise your combined
array to 0 so it will not contain anything and the second problem is that you have one for
loop to many, the one over the combined
array.
Instead you need to create a temporary array that will be large enough to cover all possible duplicates and then copy from the temporary array to an array of the right size.
public static void main(String[] args) {
int arr1[] = { 2, 4, 5, 7, 9, 10 };
int arr2[] = { 1, 2, 5, 6, 8 };
int min = Math.min(arr1.length, arr2.length); //We can never have more duplicates than the number of elements in the smallest array
int counter = 0;
int combined[] = new int[min];
for (int s = 0; s < arr1.length; s++) {
for (int x = 0; x < arr2.length; x++) {
if (arr1[s] == arr2[x]) {
combined[counter] = arr1[s];
counter++;
break; //We have found a duplicate, exit inner for loop and check next digit in outer loop
}
}
}
int[] result = Arrays.copyOf(combined, counter); //This makes a copy of the array but only with the number of elements that are used
for (int i = 0; i < result.length; i++) {
System.out.print(result[i] + " ");
}
}
If you don't want to use Arrays.copy
you can do the copying yourself, just replace that line with
int[] result = new int[counter]
for (int j = 0; j < counter; j++) {
result[j] = combined[j];
}
Upvotes: 0
Reputation: 4014
If you would like to solve this using only arrays and iterations, you can:
code:
public static void main(String[] args) {
...
// make sure input arrays are sorted
Arrays.sort(arr1);
Arrays.sort(arr2);
List<Integer> common = new ArrayList<Integer>();
int i = 0, j = 0;
while (i < arr1.length && j < arr2.length) {
int v1 = arr1[i];
int v2 = arr2[j];
if (v1 == v2) {
common.add(v1);
i++;
j++;
} else if (v1 < v2) {
i++;
} else {
j++;
}
}
System.out.println(common);
}
on each iteration you move forward:
no ArrayList version:
public static void main(String[] args) {
...
Arrays.sort(arr1);
Arrays.sort(arr2);
int found = 0;
int[] common = new int[Math.min(arr1.length, arr2.length)];
int i = 0, j = 0;
while (i < arr1.length && j < arr2.length) {
int v1 = arr1[i];
int v2 = arr2[j];
if (v1 == v2) {
common[found] = v1;
found++;
i++;
j++;
} else if (v1 < v2) {
i++;
} else {
j++;
}
}
for (int k = 0; k < found; k++) {
System.out.println("common: " + common[k]);
}
}
Upvotes: 0
Reputation: 18245
public static int[] intersection(int[] arr1, int[] arr2) {
Set<Integer> elements = IntStream.of(arr1).boxed().collect(Collectors.toSet());
return IntStream.of(arr2).filter(elements::contains).toArray();
}
Or using only int[]
:
// create temporary array to not modify input data
int[] tmp = Arrays.copyOf(arr1, arr1.length);
Arrays.sort(tmp);
for(int v : arr2)
if(Arrays.binarySearch(tmp, v) >= 0)
System.out.print(v + " ");
Upvotes: 1