把一个整形数组中重复的数字去掉

本文介绍了一种通过两次遍历整型数组来去除重复数字的方法。首先找到数组中的最大值以确定哈希表的大小,然后利用哈希表记录每个元素是否出现过,并将未重复的元素保存到新的数组中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
题目:把一个整形数组中重复的数字去掉

例如: 
1,   2,   0,   2,   -1,   999,   3,   999,   88 
答案应该是: 
1,   2,   0,   -1,   999,   3,   88

思路:一次遍历数组找出绝对值最大的数,以便确定哈希表的大小。
第二次遍历数组,当遇到第一次出现的数,则放入应该放的位置(仍在原数组中),如果重复则不放入。

ubuntu16.04中测试正确代码如下:
 **/ 

#include<iostream>
#include <stdlib.h>
#define INT_MIN     (-2147483647 - 1) /* minimum (signed) int value , (2147483647 == 0x7FFFFFF) */
using namespace std;

typedef struct Info
{
    Info():isPositive(0),isNegative(0){}
    bool isPositive;
    bool isNegative;
}HashTable;

int main()
{
    int a[] = {1,2,0,2,-1,999,3,999,88,-1,2,2,2,-1,4,4,44,444,44,444,4444};
    int max = INT_MIN;//#define INT_MIN     (-2147483647 - 1) /* minimum (signed) int value */

    for(int i=0;i<sizeof(a)/sizeof(int);++i)
    {
        cout<<a[i]<<" ";
    }
    cout<<endl;

    for(int i=0;i<sizeof(a)/sizeof(int);++i)
    {
        if(abs(a[i])>max)
            max = abs(a[i]);
    }

    HashTable* hash = new HashTable[max+1];

    int toReplace=0;
    for(int current=0;current<sizeof(a)/sizeof(int);++current)
    {
        if(hash[abs(a[current])].isPositive==0 && hash[abs(a[current])].isNegative==0)
        {
            if(a[current]>0 || a[current] == 0)
                hash[abs(a[current])].isPositive=1;
            if(a[current]<0)
                hash[abs(a[current])].isNegative=1;

            a[toReplace] = a[current];
            toReplace++;
        }
        else
        {
            //确定重复
            if(hash[abs(a[current])].isPositive==1 && (a[current]>0 || a[current]==0))
                continue ;
            if(hash[abs(a[current])].isNegative==1 && a[current]<0)
                continue ;
            else
            {//不重复
                if(a[current]>0 || a[current] == 0)
                    hash[abs(a[current])].isPositive=1;
                if(a[current]<0)
                    hash[abs(a[current])].isNegative=1;

                a[toReplace] = a[current];
                toReplace++;
            }
        }
    }

    for(int i=0;i<toReplace;++i)
    {
        cout<<a[i]<<" ";
    }
    cout<<endl;

    return 0;
}
### C语言去除数组多余元素方法 在C语言中,可以通过多种方式来处理并去除数组中的多余元素。下面介绍几种常见的方式。 #### 使用双指针法移除特定值 对于给定的一个整型数组以及目标数值,在使用额外空间的情况下可以采用双指针技术实现删除指定值的功能[^2]: ```c #include <stdio.h> int CompactIntegers(int* nums, int numsSize){ int slow = 0; for (int fast = 0; fast < numsSize; ++fast) { if (nums[fast] != 0){ // 如果是要删除的目标值,则保留下来 nums[slow++] = nums[fast]; } } return slow; // 返回的有效长度 } int main(){ int array[] = {1, 0, 2, 0, 3}; int length = sizeof(array)/sizeof(*array); int newLength = CompactIntegers(array, length); printf("New Array: "); for(int i=0;i<newLength;++i){ printf("%d ", array[i]); } return 0; } ``` 这段代码展示了如何通过遍历整个数组并将非零元素前移到前面位置从而达到去掉所有`0`的目的,并最终返回数组的有效大小。 #### 移动重复字符后的压缩字符串 针对含有重复字符的情况,也可以利用类似的思路来进行优化。这里展示了一个例子,其中涉及到的是字符类型的数组操作[^1]: ```c #include<stdio.h> int main() { char words[] = "abbbcadddd"; int len = sizeof(words) / sizeof(words[0]); for (int i = 0; i < len-1; i++) { for (int k = i + 1; k < len - 1;) { if (words[i] == words[k]) { for (int t = k; t < len - 1; t++) words[t] = words[t+1]; len--; // 数组的长度减一 } else { k++; } } } puts(words); return 0; } ``` 上述程序实现了对连续相同字母只保留第一个出现的位置,其余全部舍弃的效果。 #### 清理输入流中的冗余数据 另外一种场景是在解析来自用户的输入时可能会遇到多余的分隔符或其他需要的信息。此时可以根据实际需求设计相应的逻辑来清理这些干扰项[^3]: ```c for (int i = 1; str[i] != ']'; i++){ if (str[i] == ',') k++; // 当检测到逗号时表示完成了一次完整的数字读取过程 else arr[k] = arr[k]*10+(str[i]-'0'); // 继续累积当前正在构建的那个数位上的值 } ``` 这种方法适用于从字符串形式转换成整形数组的过程中过滤掉无关符号的过程。 综上所述,根据同应用场景可以选择合适的技术手段来有效地管理和精简数组内的内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值