java中两个数组中缺少元素
如何从两个数组中找出缺少的元素? 例:java中两个数组中缺少元素,java,Java,如何从两个数组中找出缺少的元素? 例: 从以上两个数组中,我想找到第二个数组中缺少的元素是什么?将它们转换为Sets并使用removeAll 第一个问题是如何将基元int[]转换为集合。 您可以使用: List<Integer> list1 = Ints.asList(array1); List<Integer> list2 = Ints.asList(array2); 并将结果转换回数组: return Ints.toArray(set1); 您可以使用Set及其方
从以上两个数组中,我想找到第二个数组中缺少的元素是什么?将它们转换为
Set
s并使用removeAll
第一个问题是如何将基元int[]
转换为集合。
您可以使用:
List<Integer> list1 = Ints.asList(array1);
List<Integer> list2 = Ints.asList(array2);
并将结果转换回数组:
return Ints.toArray(set1);
您可以使用Set及其方法。此操作将是一个设置差异 简单的方法是在一个数组中搜索另一个数组的每个元素(使用for循环)。如果您首先对两个数组进行排序,它会变得更加高效。考虑使用交集方法: 健康的讨论可在以下网址获得:
如果允许在数组中重复,一种有效的(O(n))解决方案可以通过迭代第一个数组来创建频率表(Map),然后使用Map匹配第二个数组中的任何元素
Map<Integer, Integer> freqMap = new HashMap<Integer, Integer>();
// Iterate over array1 and populate frequency map whereby
// the key is the integer and the value is the number of
// occurences.
for (int val1 : array1) {
Integer freq = freqMap.get(val1);
if (freq == null) {
freqMap.put(val1, 1);
} else {
freqMap.put(val1, freq + 1);
}
}
// Now read the second array, reducing the frequency for any value
// encountered that is also in array1.
for (int val2 : array2) {
Integer freq = freqMap.get(val2);
if (freq == null) {
freqMap.remove(val2);
} else {
if (freq == 0) {
freqMap.remove(val2);
} else {
freqMap.put(freq - 1);
}
}
}
// Finally, iterate over map and build results.
List<Integer> result = new LinkedList<Integer>();
for (Map.Entry<Integer, Integer> entry : freqMap.entrySet()) {
int remaining = entry.getValue();
for (int i=0; i<remaining; ++i) {
result.add(entry.getKey());
}
}
// TODO: Convert to int[] using the util. method of your choosing.
Map freqMap=newhashmap();
//迭代数组1并填充频率映射
//键是整数,值是数字
//事件。
for(int val1:array1){
整数freq=freqMap.get(val1);
if(freq==null){
freqMap.put(val1,1);
}否则{
freqMap.put(val1,freq+1);
}
}
//现在读取第二个数组,减少任何值的频率
//也在阵列1中遇到的。
for(int val2:array2){
整数freq=freqMap.get(val2);
if(freq==null){
freqMap.remove(val2);
}否则{
如果(频率==0){
freqMap.remove(val2);
}否则{
freqMap.put(freq-1);
}
}
}
//最后,迭代映射并生成结果。
列表结果=新建LinkedList();
对于(Map.Entry:freqMap.entrySet()){
int剩余=entry.getValue();
因为(inti=0;i@finnw)我相信你在想。
需要导入org.apache.commons.collections.CollectionUtils;
得到析取函数
使用该方法将查找交点中未找到的所有对象
您可以创建另外两个int数组来存储每个值的多重性。每次找到该值时,递增该值对应的数组的索引,然后比较这些数组
这可能不是最“有效”的方法,但它是一个非常简单的概念。Guava library可能很有用;您需要在Set中更改数组,然后才能使用API。这不是最有效的方法,但可能是Java中最简单的方法:
public static void main(final String[] args) {
final int[] a = { 1, 2, 3, 4, 5 };
final int[] b = { 3, 1, 2 };
// we have to do this just in case if there might some values that are missing in a and b
// example: a = { 1, 2, 3, 4, 5 }; b={ 2, 3, 1, 0, 5 }; missing value=4 and 0
findMissingValue(b, a);
findMissingValue(a, b);
}
private static void findMissingValue(final int[] x, final int[] y) {
// loop through the bigger array
for (final int n : x) {
// for each value in the a array call another loop method to see if it's in there
if (!findValueSmallerArray(n, y)) {
System.out.println("missing value: " + n);
// break;
}
}
}
private static boolean findValueSmallerArray(final int n, final int[] y) {
for (final int i : y) {
if (n == i) {
return true;
}
}
return false;
}
获取不匹配数字的简单逻辑
public static int getelements(int[] array1, int[] array2)
{
int count = 0;
ArrayList unMatched = new ArrayList();
int flag = 0;
for(int i=0; i<array1.length ; i++)
{ flag=0;
for(int j=0; j<array2.length ; j++)
{
if(array1[i] == array2[j]) {
flag =1;
break;
}
}
if(flag==0)
{
unMatched.add(array1[i]);
}
}
System.out.println(unMatched);
return unMatched.size();
}
public static void main(String[] args) {
// write your code here5
int array1[] = {7,3,7,2,8,3,2,5};
int array2[] = {7,4,9,5,5,10,4};
int count;
count = getelements(array1,array2);
System.out.println(count);
}
publicstaticintgetelements(int[]array1,int[]array2)
{
整数计数=0;
ArrayList unMatched=新建ArrayList();
int标志=0;
对于(int i=0;ii如果你已经知道一些编程/计算机科学知识,我会选择不属于我的答案。如果你只是在学习,试着自己写出所有代码,这样你就能理解它。两个数组中都允许重复值吗?这假设两个数组中都不允许重复。我正在考虑一种转换的方法int[]
到Integer[]
或者到List
。我不知道析取(我认为它在番石榴中还没有等价物。)@finnw我认为列表中没有一个,但commons lang的ArrayUtils.toObject(int[]数组)返回整数[]。请解释你的代码是做什么的,以及它是如何做的。方法getelements需要2个数组。现在使用2个循环将有助于比较数组1的元素和数组2的元素。如果条件匹配,我将打破该循环。并将其插入数组列表,因为允许重复。就是这样。
Map<Integer, Integer> freqMap = new HashMap<Integer, Integer>();
// Iterate over array1 and populate frequency map whereby
// the key is the integer and the value is the number of
// occurences.
for (int val1 : array1) {
Integer freq = freqMap.get(val1);
if (freq == null) {
freqMap.put(val1, 1);
} else {
freqMap.put(val1, freq + 1);
}
}
// Now read the second array, reducing the frequency for any value
// encountered that is also in array1.
for (int val2 : array2) {
Integer freq = freqMap.get(val2);
if (freq == null) {
freqMap.remove(val2);
} else {
if (freq == 0) {
freqMap.remove(val2);
} else {
freqMap.put(freq - 1);
}
}
}
// Finally, iterate over map and build results.
List<Integer> result = new LinkedList<Integer>();
for (Map.Entry<Integer, Integer> entry : freqMap.entrySet()) {
int remaining = entry.getValue();
for (int i=0; i<remaining; ++i) {
result.add(entry.getKey());
}
}
// TODO: Convert to int[] using the util. method of your choosing.
Integer[] array1 ={1,2,3,4,5};
Integer[] array2 ={3,1,2};
List list1 = Arrays.asList(array1);
List list2 = Arrays.asList(array2);
Collection result = CollectionUtils.disjunction(list1, list2);
System.out.println(result); // displays [4, 5]
public static void main(final String[] args) {
final int[] a = { 1, 2, 3, 4, 5 };
final int[] b = { 3, 1, 2 };
// we have to do this just in case if there might some values that are missing in a and b
// example: a = { 1, 2, 3, 4, 5 }; b={ 2, 3, 1, 0, 5 }; missing value=4 and 0
findMissingValue(b, a);
findMissingValue(a, b);
}
private static void findMissingValue(final int[] x, final int[] y) {
// loop through the bigger array
for (final int n : x) {
// for each value in the a array call another loop method to see if it's in there
if (!findValueSmallerArray(n, y)) {
System.out.println("missing value: " + n);
// break;
}
}
}
private static boolean findValueSmallerArray(final int n, final int[] y) {
for (final int i : y) {
if (n == i) {
return true;
}
}
return false;
}
public static int getelements(int[] array1, int[] array2)
{
int count = 0;
ArrayList unMatched = new ArrayList();
int flag = 0;
for(int i=0; i<array1.length ; i++)
{ flag=0;
for(int j=0; j<array2.length ; j++)
{
if(array1[i] == array2[j]) {
flag =1;
break;
}
}
if(flag==0)
{
unMatched.add(array1[i]);
}
}
System.out.println(unMatched);
return unMatched.size();
}
public static void main(String[] args) {
// write your code here5
int array1[] = {7,3,7,2,8,3,2,5};
int array2[] = {7,4,9,5,5,10,4};
int count;
count = getelements(array1,array2);
System.out.println(count);
}