删除数组中的重复元素而不进行排序-错误的输出

时间:2018-08-30 09:34:48

标签: c arrays output

我一直在尝试编写一个函数来删除int数组中的重复元素而不进行排序。

对于该任务,我创建了一个名为removeDuplicateElements的函数,该函数获取一个数组及其字符串,并返回一个新的动态分配的数组,该数组是原始数组的副本,其中删除了所有重复元素。此函数还通过引用返回新数组的大小。

我还在代码函数中使用了构建动态数组并将其打印的方法。

这是我的代码:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

void printArray(int *arr, int size);
int *buildArray(int size);
int *removeDuplicateElements(int *arr, int size, int *newSize);

void main() {
    int size,newSize;
    int *arr;
    int *newArr;

    printf("please enter a number for the size of array: ");
    scanf("%d", &size);
    printf("\nenter %d numbers: ", size);
    arr = buildArray(size);
    printf("\nthe array after removing the duplicate elements is: ");
    newArr = removeDuplicateElements(arr, size, &newSize);
    printArray(newArr, newSize);
    free(newArr);
    free(arr);
}

/* this function removes all duplicate elements in a given array */
int *removeDuplicateElements(int *arr, int size, int *newSize) {
    int *newArr;
    int count = size, i, j; 

    /* finding the new size of the original array with removal its duplicate elements */
    for (i = 1; i < size; i++) {  
        for (j = 0; j < size; j++)
            if (arr[i] == arr[j] && i != j) {
                count--;
                break;
            }
    }
    newArr = (int*)malloc(count * sizeof(int)); /* dynamically allocating the new array */

    count = 1;
    newArr[0] = arr[0];

    /*adding the elements in the new array without changing the order*/
    for (i = 1; i < size; i++) {
        for (j = 0; j < size; j++) {
            if (arr[i] == arr[j] && i != j) {
                break;
            }
            if (j == size - 1) {
                newArr[count] = arr[i];
                count++;
            }
        }
    }
    *newSize = count;     /* updating the size of the new array */
    return newArr;        /* returning the address of new array */
}

void printArray(int *arr, int size) {
    int i;
    for (i = 0; i < size; i++)
        printf("%d ", arr[i]);
    printf("\n");
}

int *buildArray(int size) {
    int i;
    int *arr = (int*)malloc(size * sizeof(int));

    if (!arr) {
        printf("ERROR! Not enough memory!\n");
        exit(1);
    }

    for (i = 0; i < size; i++)
        scanf("%d", &arr[i]);

    return arr;
}

该代码输出错误,我不明白为什么

例如,对于以下带有size=5的数组:1 1 3 1 3 我得到了错误的输出1,而预期的输出是 1 3

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:3)

您首先是错误地计算了新数组的大小。对于示例输入,当您查看前3个时,它将扫描整个数组以查看有3个,并找到2个,并得出结论是重复的。然后,它对第二个3执行完全相同的操作。因此,最终得到的新数组的大小为1。

您不必扫描整个数组,而只想扫描数组中要检查的元素之前的元素。像这样。

for(i=1;i<size;i++)
{
    for (j = 0; j < i; j++)
        if (arr[i] == arr[j])
        {
            count--;
            break;
        }
}

对于填充新数组的代码也有同样的问题

for(i=1;i<size;i++)
{
    for (j = 0; j < i; j++)
        if (arr[i] == arr[j])
        {
            break;
        }
    if(j==i)
    {
        newArr[count++]=arr[i];
    }
}

答案 1 :(得分:0)

还有一种替代方法,当然它涉及修改原始数组,但这只是一种替代方法。基本上,它涉及通过将最大值替换为0xFFFF来消除重复元素。

int* removeDuplicateElements(int *arr, int size, int *newSize)
{    
int *newArr;
int count = size, i, j;
int index = 0;

/*finding the new size of the original array with removal its duplicate elements*/

for(i=0;i<size;i++)
{
    for (j = i+1; j < size; j++)
        if (arr[i] == arr[j] && arr[i] != 0xFFFF)
            {
                count--;
                arr[j] = 0xFFFF;

            }

}

printf("Size is %d \n", count);

newArr = (int*)malloc(count * sizeof(int));      /*dynamically allocating the new array*/

for(i=0;i<size;i++)
{
    if(arr[i] != 0xFFFF)
    newArr[index++] = arr[i];
}

*newSize = count;     /*updating the size of the new array*/
return newArr;        /*returning the address of new array*/

}