I'm writing a function which returns the difference between two integer arrays. I'm assuming that that all elements in input arrays are unique and also the input arrays are not sorted. For Example:
我正在编写一个返回两个整数数组之间差异的函数。我假设输入数组中的所有元素都是唯一的,输入数组也没有排序。例如:
Input:arr1 = [1,2,3,5,4]
arr2 = [1,2,3]
输入:arr1 = [1,2,3,5,4] arr2 = [1,2,3]
Expected Output: [4,5]
预期产出:[4,5]
My Output: [1,2,3,4,5]
(when first array is larger than second)
我的输出:[1,2,3,4,5](当第一个数组大于第二个时)
When I make the second array larger than first, I get ArrayIndexOutOfBoundsException
.
当我使第二个数组大于第一个数组时,我得到ArrayIndexOutOfBoundsException。
public class Test{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter length of first array");
int ml = sc.nextInt();
System.out.println("Enter length of second array");
int nl = sc.nextInt();
int m[] = new int[ml];
int n[] = new int[nl];
System.out.println("Enter elements of first array");
for(int i=0;i<ml;i++)
{
m[i] = sc.nextInt();
}
System.out.println("Enter elements of second array");
for(int j=0;j<nl;j++)
{
m[j] = sc.nextInt();
}
ArrayList<Integer> arr1 = new ArrayList<Integer>();
for(int i: m){ arr1.add(i);}
ArrayList<Integer> arr2 = new ArrayList<Integer>();
for(int j: n){ arr2.add(j);}
if(ml>nl)
{
arr1.removeAll(arr2);
System.out.println(arr1);
}
else
{
arr2.removeAll(arr1);
System.out.println(arr2);
}
}
}
4 个解决方案
#1
2
In the second iteration you should use n[j] = ...
instead of m[j] = ...
You should use more descriptive variable names to prevent thing kind of thing from happening.
在第二次迭代中,你应该使用n [j] = ...而不是m [j] = ...你应该使用更多的描述性变量名来防止事情发生。
#2
0
You can check if element exist in second one if not can add to output array as below:
您可以检查第二个元素是否存在,如果不存在,可以添加到输出数组,如下所示:
int[] array1 = new int[] { 1, 2, 3, 4, 5 };
int[] array2 = new int[] { 1, 2, 3 };
List<Integer> output = new ArrayList<>();
for (int i = 0; i < array1.length; i++) {
boolean flag = false;
for (int j = 0; j < array2.length; j++) {
if (array1[i] == array2[j]) {
flag = true;
break;
}
}
if (!flag) {
output.add(array1[i]);
}
}
System.out.println(output);
#3
0
If it is not a homework that let you practice on array/iterations, you may consider using Set
for much simpler logic:
如果它不是一个让你练习数组/迭代的作业,你可以考虑使用Set来实现更简单的逻辑:
// pseudo-code
Set<Integer> set1 = new HashSet<>(array1);
Set<Integer> set2 = new HashSet<>(array2);
// Using Guava
Set<Integer> diff = Sets.symmetricDifference(set1, set2);
// Java only
Set<Integer> diff1 = new HashSet<>(set1);
diff1.removeAll(set2); // diff1 contains entries in array1 but not 2
Set<Integer> diff2 = new HashSet<>(set2);
diff2.removeAll(set1); // diff2 contains entries in array2 but not 1
Set<Integer> diff = new HashSet<>(set1);
diff.addAll(set2);
// Java only, using stream
return Stream.concat(set1.stream(), set2.stream())
.filter(i -> ! (set1.contains(i) && set2.contains(i)))
.collect(Collectors.toSet());
#4
-3
Well the first idea that comes to my mind is something like this:
我想到的第一个想法是这样的:
if(array1.Length>array2.Length){
foreach(int k in array1){
foreach(int l in array2){
if(k==l){
k=null;
}
}
}
}else{
foreach(int l in array2){
foreach(int k in array1){
if(l==k){
l=null;
}
}
}
}
Untested, and only works in theorem for when one array is a subset of another, but hopefully its a start :)
未经测试,仅适用于当一个数组是另一个数组的子集时的定理,但希望它是一个开始:)
#1
2
In the second iteration you should use n[j] = ...
instead of m[j] = ...
You should use more descriptive variable names to prevent thing kind of thing from happening.
在第二次迭代中,你应该使用n [j] = ...而不是m [j] = ...你应该使用更多的描述性变量名来防止事情发生。
#2
0
You can check if element exist in second one if not can add to output array as below:
您可以检查第二个元素是否存在,如果不存在,可以添加到输出数组,如下所示:
int[] array1 = new int[] { 1, 2, 3, 4, 5 };
int[] array2 = new int[] { 1, 2, 3 };
List<Integer> output = new ArrayList<>();
for (int i = 0; i < array1.length; i++) {
boolean flag = false;
for (int j = 0; j < array2.length; j++) {
if (array1[i] == array2[j]) {
flag = true;
break;
}
}
if (!flag) {
output.add(array1[i]);
}
}
System.out.println(output);
#3
0
If it is not a homework that let you practice on array/iterations, you may consider using Set
for much simpler logic:
如果它不是一个让你练习数组/迭代的作业,你可以考虑使用Set来实现更简单的逻辑:
// pseudo-code
Set<Integer> set1 = new HashSet<>(array1);
Set<Integer> set2 = new HashSet<>(array2);
// Using Guava
Set<Integer> diff = Sets.symmetricDifference(set1, set2);
// Java only
Set<Integer> diff1 = new HashSet<>(set1);
diff1.removeAll(set2); // diff1 contains entries in array1 but not 2
Set<Integer> diff2 = new HashSet<>(set2);
diff2.removeAll(set1); // diff2 contains entries in array2 but not 1
Set<Integer> diff = new HashSet<>(set1);
diff.addAll(set2);
// Java only, using stream
return Stream.concat(set1.stream(), set2.stream())
.filter(i -> ! (set1.contains(i) && set2.contains(i)))
.collect(Collectors.toSet());
#4
-3
Well the first idea that comes to my mind is something like this:
我想到的第一个想法是这样的:
if(array1.Length>array2.Length){
foreach(int k in array1){
foreach(int l in array2){
if(k==l){
k=null;
}
}
}
}else{
foreach(int l in array2){
foreach(int k in array1){
if(l==k){
l=null;
}
}
}
}
Untested, and only works in theorem for when one array is a subset of another, but hopefully its a start :)
未经测试,仅适用于当一个数组是另一个数组的子集时的定理,但希望它是一个开始:)