如何從兩個數組中找出缺失的元素? 例如:在java中缺少兩個數組中的元素
int []array1 ={1,2,3,4,5};
int []array2 ={3,1,2};
從以上兩個數組我想找到什麼是第二個數組缺少的元素?
如何從兩個數組中找出缺失的元素? 例如:在java中缺少兩個數組中的元素
int []array1 ={1,2,3,4,5};
int []array2 ={3,1,2};
從以上兩個數組我想找到什麼是第二個數組缺少的元素?
將它們轉換爲Set
s並使用removeAll
。
第一個問題是如何將原始int[]
轉換爲集合。 隨着Guava你可以使用:
List<Integer> list1 = Ints.asList(array1);
List<Integer> list2 = Ints.asList(array2);
阿帕奇百科全書(這我不熟悉)顯然有類似的東西。
現在轉換成一組:
Set<Integer> set1 = new HashSet<Integer>(list1);
,並計算不同:
set1.removeAll(list2);
而結果轉換回一個數組:
return Ints.toArray(set1);
這假定兩個陣列中都不允許有重複項。 – Adamski 2010-02-05 14:09:29
可以使用SET和其方法。這個操作將會有一定的差異。
簡單的方法是簡單地搜索一個數組中的每個元素(使用for循環)。如果你第一次將兩個陣列分開,它變得更有效率。
如果被允許重複的陣列,一個有效的(O(n))的溶液以創建一個頻率表(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.
@finnw我相信你在考慮commons-collections。 需要導入org.apache.commons.collections.CollectionUtils; 獲取析取函數。
使用disjunction方法會發現,沒有在路口找到的所有對象。
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]
我正在考慮將'int []'轉換爲'Integer []'或者'List
@finnw我認爲列表
您可以創建另外兩個int數組來存儲每個值的多重性。每次找到數值時,增加數組對應的數組索引,然後比較數組。也許它不是最「高效」的方式,但它是一個非常簡單的概念。
番石榴圖書館可以幫助;您需要更改Set中的Array,然後才能使用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;
}
如果你已經知道了一些編程/計算機科學,我會與不是我的答案去。如果你只是在學習,儘量自己寫出所有的代碼,以便你理解它。 – 2010-02-05 14:07:15
您是否允許在兩個數組中使用重複值? – Adamski 2010-02-05 14:09:47