# Create a method that with 2 given arrays returns a new one with elements from a and b with no duplicates:

go评论60阅读模式

Create a method that with 2 given arrays returns a new one with elements from a and b with no duplicates:

# 问题

``````public static int[] arrayUnion(int[] a, int[] b) {
int[] result;

for (int i = 0; i < a.length; i++) {
for (int k = 0; k < result.length; k++) {
if (a[i] != result[k]) {
}
}
}

for (int j = 0; j < b.length; j++) {
for (int k = 0; k < result.length; k++) {
if (b[j] != result[k]) {
}
}
}
return result;
}
``````

Hey everyone I am a beginner and I am trying to do this problem, here is what I have tried so far...I am really stumped though:

Write a method called int[] arrayUnion(int[] a, int[] b) that, given two int arrays, returns a new array with unique elements from a and b. No duplicates!

Example Outputs:
arrayUnion([1, 2, 3], [-1, 0, 1]) returns [1, 2, 3, -1, 0]

arrayUnion([1, 1, 1], [1, 1, 1]) returns 1

``````   public static int[] arrayUnion(int[] a, int[] b){
int[] result;

for(int i =0; i&lt; a.length; i++){
for(int k =0; k&lt;result.length; k++){
if(a[i]!= result[k]){
}
}
}

for(int j =0; j&lt; b.length; j++){
for(int k =0; k&lt;result.length; k++){
if(b[j]!=result[k]){
}
}
}
return result;
}
``````

# 答案1

``````int[] a = { 1, 2, 3, 2, 7, 4, 5 };
int[] b = { 2, 9, 4, 5, 5 };

int[] union = arrayUnion(a, b);
System.out.println(Arrays.toString(union));
``````

``````[1, 2, 3, 4, 5, 7, 9]
``````

• 将两个数组转换成 `IntStream` 流。
• 去除重复值。
• 再将流转换回 `int` 数组。
``````static int[] arrayUnion(int[] a, int[] b) {
return Stream.of(a, b).flatMapToInt(IntStream::of)
.distinct()
.toArray();
}
``````

You can do it with a stream, merging the two arrays keeping only the distinct values single array.

``````int[] a = { 1, 2, 3, 2, 7, 4, 5 };
int[] b = { 2, 9, 4, 5, 5 };

int[] union = arrayUnion(a,b);
System.out.println(Arrays.toString(union));

``````

prints

``````[1, 2, 3, 4, 5, 7, 9]
``````

This works by

• flattening the two arrays into an `Intstream`.
• getting rid of duplicates
• and converting back to an `int array`.
``````static int[] arrayUnion(int[] a, int[] b) {
return Stream.of(a,b).flatMapToInt(IntStream::of)
.distinct()
.toArray();
}

</details>

# 答案2
**得分**: 0

```java
public int[] arrayUnion(int[] a, int[] b) {
List<Integer> result = new ArrayList<>();

for (int value: a) {
if (!result.contains(value)) {
}
}

for (int value: b) {
if (!result.contains(value)) {
}
}

return result.stream().mapToInt(Integer::intValue).toArray();
}
``````

``````public int[] arrayUnion(int[] a, int[] b) {
Set<Integer> result = new HashSet<>();

for (int value: a) {
}

for (int value: b) {
}

return result.stream().mapToInt(Integer::intValue).toArray();
}
``````

``````public int[] arrayUnion(int[] a, int[] b) {
return Stream.of(a, b).flatMapToInt(IntStream::of).distinct().toArray();
}
``````

First you can not use the `add` method for an array. So therefore your code could look like this:

``````public int[] arrayUnion(int[] a, int[] b) {
List&lt;Integer&gt; result = new ArrayList&lt;&gt;();

for (int value: a) {
if (!result.contains(value)) {
}
}

for (int value: b) {
if (!result.contains(value)) {
}
}

return result.stream().mapToInt(Integer::intValue).toArray();
}
``````

Second it is a little bit easier with a set. Therefore you can easily add all values and return the array:

``````public int[] arrayUnion(int[] a, int[] b) {
Set&lt;Integer&gt; result = new HashSet&lt;&gt;();

for (int value: a) {
}

for (int value: b) {
}

return result.stream().mapToInt(Integer::intValue).toArray();
}
``````

And there is also a third option. You can use the Java Stream API with the flatMap:

``````public int[] arrayUnion(int[] a, int[] b) {
return Stream.of(a, b).flatMapToInt(IntStream::of).distinct().toArray();
}
``````

# 答案3

``````public static int[] arrayUnion(int[] a, int[] b){

HashSet<Integer> set = new HashSet();

for(int i = 0; i < a.length; i++){
}

for(int i = 0; i < b.length; i++){
}

int n = set.size();
int[] result = new int[n];

int j = 0;
for(Integer i : set) {
result[j++] = i;
}

return result;
}
``````

If you can use the Java Collections, you can use Set as follows to get the unique integers array:

``````public static int[] arrayUnion(int[] a, int[] b){

HashSet&lt;Integer&gt; set=new HashSet();

for(int i =0; i&lt; a.length; i++){
}

for(int i =0; i&lt; b.length; i++){
}

int n = s.size();
int[] result; = new String[n];

int j = 0;
for(Integer i: set) {
result[j++] = i;
}

return result;
}
``````

• 本文由 发表于 2020年10月5日 00:44:16
• 转载请务必保留本文链接：https://go.coder-hub.com/64197284.html
• java

go 56

go 65

go 59

go 60