【发布时间】:2017-10-04 03:33:28
【问题描述】:
我正在尝试创建一个函数来清除数组中的数字重复项,但似乎我无法弄清楚我缺少什么来删除重复项。 只是为了更清楚: 该函数不应变为无效。
[1,2,3,3,4] -> [1,2,3,4]
[4,2,5,1]->[4,2,5,1]
[32,21,2,5,2,1,21,4]->[32,21,2,5,1,4]
我的数组中不应该是空格,函数应该返回 已清理数组中的唯一元素,其中已清理被定义为“整数不重复”
#include <stdio.h>
int generateUniqeList(int *list, int length);
int main()
{
int list[6] = { 5, 5, 4, 3, 2, 1 };
int duplicate = generateUniqeList(list, 6);
for (int i = 0; i < 6; i++)
{
printf("%d\n", list[i] - 1); //Here i am able to change the value of the duplicates with the - 1
}
getchar();
return 0;
}
int generateUniqeList(int *list, int length)
{
int duplicate = 0;
for (int i = 0; i < length; i++)
{
if (list[i] == list[i])
duplicate = list[i];
}
return duplicate;
}
【问题讨论】:
-
对于
i的某些值,您是否期望此条件if (list[i] == list[i])评估为false? -
@jhhoff02 这不是客观的 c
-
元素值有上限吗?
-
要删除一个 int,您必须将整个数组从要删除的当前位置向左移动一个索引。
-
@Module 假设您不想就地执行此操作。我会生成一个新列表。
标签: c arrays function duplicates