英文:
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]) {
result.add(a[i]);
}
}
}
for (int j = 0; j < b.length; j++) {
for (int k = 0; k < result.length; k++) {
if (b[j] != result[k]) {
result.add(b[j]);
}
}
}
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< a.length; i++){
for(int k =0; k<result.length; k++){
if(a[i]!= result[k]){
result.add(a[i]);
}
}
}
for(int j =0; j< b.length; j++){
for(int k =0; k<result.length; k++){
if(b[j]!=result[k]){
result.add(b[j]);
}
}
}
return result;
}
答案1
得分: 2
你可以使用流来实现,将两个数组合并,只保留唯一值,形成一个单一的数组。
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
首先,您不能对数组使用`add`方法。因此,您的代码可能如下所示:
```java
public int[] arrayUnion(int[] a, int[] b) {
List<Integer> result = new ArrayList<>();
for (int value: a) {
if (!result.contains(value)) {
result.add(value);
}
}
for (int value: b) {
if (!result.contains(value)) {
result.add(value);
}
}
return result.stream().mapToInt(Integer::intValue).toArray();
}
其次,使用集合会更加简便。因此,您可以轻松地添加所有值并返回数组:
public int[] arrayUnion(int[] a, int[] b) {
Set<Integer> result = new HashSet<>();
for (int value: a) {
result.add(value);
}
for (int value: b) {
result.add(value);
}
return result.stream().mapToInt(Integer::intValue).toArray();
}
还有第三个选项。您可以使用Java Stream API与flatMap方法:
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<Integer> result = new ArrayList<>();
for (int value: a) {
if (!result.contains(value)) {
result.add(value);
}
}
for (int value: b) {
if (!result.contains(value)) {
result.add(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<Integer> result = new HashSet<>();
for (int value: a) {
result.add(value);
}
for (int value: b) {
result.add(value);
}
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
得分: 0
如果您可以使用Java集合框架,可以按照以下方式使用Set来获取唯一的整数数组:
public static int[] arrayUnion(int[] a, int[] b){
HashSet<Integer> set = new HashSet();
for(int i = 0; i < a.length; i++){
set.add(a[i]);
}
for(int i = 0; i < b.length; i++){
set.add(b[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<Integer> set=new HashSet();
for(int i =0; i< a.length; i++){
set.add(a[i]);
}
for(int i =0; i< b.length; i++){
set.add(b[i]);
}
int n = s.size();
int[] result; = new String[n];
int j = 0;
for(Integer i: set) {
result[j++] = i;
}
return result;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论