gpt4 book ai didi

java - 如何删除数组合并方法中的重复项?

转载 作者:太空宇宙 更新时间:2023-11-04 10:42:59 25 4
gpt4 key购买 nike

我有一个方法,它接受 2 个属性,即 2 个数组,并按升序合并它们。剩下的就是弄清楚如何删除重复项。代码如下:

public static int[] mergeArrays(int[] a, int[] b){

int[] c = new int[a.length+b.length];
int aIt = 0;
int bIt = 0;

while(true) {


if(aIt < a.length && bIt < b.length) {
if(a[aIt] == b[bIt]){
c[aIt+bIt] = a[aIt++];
}
else{
c[aIt+bIt] = b[bIt++];
}
} else if(aIt < a.length) {
c[aIt+bIt] = a[aIt++];
} else if(bIt < b.length) {
c[aIt+bIt] = b[bIt++];
} else {
break;
}
}
return c;
}

正如你所想象的,这是一项作业,所以我不应该使用任何外部库,否则这会容易得多。

我尝试了这种方法,但是当我在控制台中运行此代码时,它似乎将其放入一些永无休止的循环中,该循环完全消耗了我的 CPU 核心,直到我停止该进程:

public static int[] mergeArrays(int[] a, int[] b){

int[] c = new int[a.length+b.length];
int aIt = 0;
int bIt = 0;
int lastVal = 0;
while(true) {
if(c[aIt+bIt] == lastVal){
continue;
}
else{
if(aIt < a.length && bIt < b.length) {
if(a[aIt] == b[bIt]){
c[aIt+bIt] = a[aIt++];
lastVal = c[aIt+bIt];
}
else{
c[aIt+bIt] = b[bIt++];
lastVal = c[aIt+bIt];
}
} else if(aIt < a.length) {
c[aIt+bIt] = a[aIt++];
lastVal = c[aIt+bIt];
} else if(bIt < b.length) {
c[aIt+bIt] = b[bIt++];
lastVal = c[aIt+bIt];
} else {
break;
}
}
}
return c;
}

似乎“继续”关键字是问题所在。当我尝试在其位置中断时,代码就会执行。

编辑:

我已向 mergeArrays 方法添加了一个新数组:

public static int[] mergeArrays(int[] a, int[] b)
{
int a_size = a.length;
int b_size = b.length;
int[] c = new int[a_size + b_size];
int[] d = null;
int i = 0 , j = 0, x = -1;
for(; i < a_size && j < b_size;)
{
if(a[i] <= b[j])
{
c[++x] = a[i];
++i;
}
else
{
if(c[x] != b[j])
{
c[++x] = b[j]; // avoid duplicates
}
++j;
}
}
--i; --j;
while(++i < a_size)
{
c[++x] = a[i];
}
while(++j < b_size)
{
c[++x] = b[j];
}
d = new int[uniqueValues(c)];

for(int g=0; g<uniqueValues(c); g++){
d[g] = c[g];
}
return d;
}

最佳答案

那么您当然可以通过以下两个步骤完成这些任务:

Sort both the arrays.

static void sortArray(int[] a) {
for (int lastPos = a.length - 1; lastPos >= 0; lastPos--) {
for (int index = 0; index <= lastPos - 1; index++) {
if (a[index] > a[index + 1]) {
int temp = a[index];
a[index] = a[index + 1];
a[index + 1] = temp;
}
}
}
}

Remove duplicates while merging them.

public static int[] mergeArrays(int[] a, int[] b)
{
int a_size = a.length;
int b_size = b.length;
int[] c = new int[a_size + b_size];
int i = 0 , j = 0, x = -1;
for(; i < a_size && j < b_size;)
{
if(a[i] <= b[j])
{
c[++x] = a[i];
++i;
}
else
{
if(c[x] != b[j])
{
c[++x] = b[j]; // avoid duplicates
}
++j;
}
}
--i; --j;
while(++i < a_size)
{
c[++x] = a[i];
}
while(++j < b_size)
{
c[++x] = b[j];
}
return c;
}

Count unique values in newly merged array.

static int uniqueValues(int[] c) {
int uniqueValues = c.length;
for (int i = 0; i < c.length; i++) {
while (c[i] == c[i + 1] && i + 1 < c.length && c[i] > c[i+1] ) {
i++;
uniqueValues--;
}
}
return unique;
}

Reestablish the merged array with unique value count i.e. delete the remaining elements after count finishes.

sortArray(a);
sortArray(b);
int[] c = mergeArrays(a, b);
c = deleteDuplicateEntries(c, uniqueValues(c));

如果您觉得它有用,请告诉我。

关于java - 如何删除数组合并方法中的重复项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48732826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com