这个问题在这里已经有答案了:
8年前关门了。可能重复:java中数组列表的交/并您好,我有两个字符串数组。我想打印两个数组之间的差异。有什么java方法吗?例如;
String[ ] first={"A","B","C"};String[ ] second={"C","B"};
String[ ] first={"A","B","C"};
String[ ] second={"C","B"};
结果必须是“a”。谢谢大家的评论。
1sbrub3j1#
将数组转换为 Set<String> ```new HashSet(Arrays.asList(array));
Set<String>
做什么
Set commonOnes = biggerSet.retainAll(smallerSet);biggerSet.removeAll(commonOnes).add(smallerSet.removeAll(commonOnes))
或者用Guava `difference()`
nimxete22#
这件衣服穿进去了 O(n log n + m log m) ,在哪里 n 大小是 first ,和 m 大小是 second . 基本上,它对数组进行排序,然后遍历每个数组,添加与数组不匹配的数组 LinkedList 在每个机会,然后在最后做一个数组。此代码的早期版本无法正常工作,因为较长列表中的尾部元素没有添加到末尾。
O(n log n + m log m)
n
first
m
second
LinkedList
public class SetDifference { public static void main(String... args) { String[] arrA = {"1", "2", "3", "4", "5", "25", "10"}; String[] arrB = {"1", "2", "10", "4", "30"}; System.out.println(Arrays.toString(differences(arrA, arrB))); } public static String[] differences(String[] first, String[] second) { String[] sortedFirst = Arrays.copyOf(first, first.length); // O(n) String[] sortedSecond = Arrays.copyOf(second, second.length); // O(m) Arrays.sort(sortedFirst); // O(n log n) Arrays.sort(sortedSecond); // O(m log m) int firstIndex = 0; int secondIndex = 0; LinkedList<String> diffs = new LinkedList<String>(); while (firstIndex < sortedFirst.length && secondIndex < sortedSecond.length) { // O(n + m) int compare = (int) Math.signum(sortedFirst[firstIndex].compareTo(sortedSecond[secondIndex])); switch(compare) { case -1: diffs.add(sortedFirst[firstIndex]); firstIndex++; break; case 1: diffs.add(sortedSecond[secondIndex]); secondIndex++; break; default: firstIndex++; secondIndex++; } } if(firstIndex < sortedFirst.length) { append(diffs, sortedFirst, firstIndex); } else if (secondIndex < sortedSecond.length) { append(diffs, sortedSecond, secondIndex); } String[] strDups = new String[diffs.size()]; return diffs.toArray(strDups); } private static void append(LinkedList<String> diffs, String[] sortedArray, int index) { while(index < sortedArray.length) { diffs.add(sortedArray[index]); index++; } }}
public class SetDifference {
public static void main(String... args) {
String[] arrA = {"1", "2", "3", "4", "5", "25", "10"};
String[] arrB = {"1", "2", "10", "4", "30"};
System.out.println(Arrays.toString(differences(arrA, arrB)));
}
public static String[] differences(String[] first, String[] second) {
String[] sortedFirst = Arrays.copyOf(first, first.length); // O(n)
String[] sortedSecond = Arrays.copyOf(second, second.length); // O(m)
Arrays.sort(sortedFirst); // O(n log n)
Arrays.sort(sortedSecond); // O(m log m)
int firstIndex = 0;
int secondIndex = 0;
LinkedList<String> diffs = new LinkedList<String>();
while (firstIndex < sortedFirst.length && secondIndex < sortedSecond.length) { // O(n + m)
int compare = (int) Math.signum(sortedFirst[firstIndex].compareTo(sortedSecond[secondIndex]));
switch(compare) {
case -1:
diffs.add(sortedFirst[firstIndex]);
firstIndex++;
break;
case 1:
diffs.add(sortedSecond[secondIndex]);
secondIndex++;
default:
if(firstIndex < sortedFirst.length) {
append(diffs, sortedFirst, firstIndex);
} else if (secondIndex < sortedSecond.length) {
append(diffs, sortedSecond, secondIndex);
String[] strDups = new String[diffs.size()];
return diffs.toArray(strDups);
private static void append(LinkedList<String> diffs, String[] sortedArray, int index) {
while(index < sortedArray.length) {
diffs.add(sortedArray[index]);
index++;
2条答案
按热度按时间1sbrub3j1#
将数组转换为
Set<String>
```new HashSet(Arrays.asList(array));
Set commonOnes = biggerSet.retainAll(smallerSet);
biggerSet.removeAll(commonOnes).add(smallerSet.removeAll(commonOnes))
nimxete22#
这件衣服穿进去了
O(n log n + m log m)
,在哪里n
大小是first
,和m
大小是second
. 基本上,它对数组进行排序,然后遍历每个数组,添加与数组不匹配的数组LinkedList
在每个机会,然后在最后做一个数组。此代码的早期版本无法正常工作,因为较长列表中的尾部元素没有添加到末尾。