首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

剔除一个数组中的重复元素,并输出(初学)

2013-03-06 
删除一个数组中的重复元素,并输出(初学)1.我第一次看到之后想的是排序,然后发现排序之后还很麻烦,太复杂;2

删除一个数组中的重复元素,并输出(初学)
1.我第一次看到之后想的是排序,然后发现排序之后还很麻烦,太复杂;
2.之后想的是先选第一个元素,后面与之比较,遇到重复的就删除,然后依次第二个,第三个。。代码写出来想好没那么简单,问题是:每次循环都要知道目前数组长度。这个算法应该小于On^2吧
3.后来不知道怎么想到,可以将数组构造二叉搜索树。假如遇到相同的元素,就不插入,就continue,继续选下一个元素。算法好像是O(nlgn)。代码部分更不知道了,记得这只学算法,没去记代码。
   希望帮忙各位帮我看看这些的可行性,还有比如好的算法也更好,代码部分的实现对我来说也是问题。

算法
[解决办法]
遍历一次:
array[n];
for(i = 0; i<n; i++)
{
  for(j = i; i<n; j++)
  {
    if(array[i] == array[j])
    {
      array[j] = -1;//定义一个标记
    }
  }
}

剩下的非-1的就是了
[解决办法]
如果是整数,并且范围不是太大,可以用计数法,复杂度 O(N)
比如:数据范围是0-65535,数据个数N,保存在Value[N]中

int CNT[65536] = {0};
for (int i = 0; i < N; ++i)
{
    ++CNT[Value[i]];
}
for (int x = 0; x < 65536; ++x)
{
    if (CNT[x] > 0)
    {
        cout << x << endl;
    }
}
[解决办法]
仅供参考

//文件1中的内容排序并去重,结果保存到文件2中
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXCHARS 128      //能处理的最大行宽,包括行尾的\n和字符串尾的\0
int MAXLINES=10000,MAXLINES2;
char *buf,*buf2;
int c,n,hh,i,L;
FILE *f;
char ln[MAXCHARS];
int ignore_case=0;
int icompare(const void *arg1,const void *arg2) {
   return stricmp((char *)arg1,(char *)arg2);
}
int compare(const void *arg1,const void *arg2) {
   return strcmp((char *)arg1,(char *)arg2);
}
int main(int argc,char **argv) {
    if (argc<3) {
        printf("Unique line. Designed by zhao4zhong1@163.com. 2012-08-20\n");
        printf("Usage: %s src.txt uniqued.txt [-i]\n",argv[0]);
        return 1;
    }
    if (argc>3) ignore_case=1;//若存在命令行参数3,忽略大小写
    f=fopen(argv[1],"r");
    if (NULL==f) {
        printf("Can not find file %s!\n",argv[1]);
        return 1;
    }
    buf=(char *)malloc(MAXLINES*MAXCHARS);
    if (NULL==buf) {
        fclose(f);
        printf("Can not malloc(%d LINES*%d CHARS)!\n",MAXLINES,MAXCHARS);
        return 2;
    }
    n=0;
    hh=0;
    i=0;
    while (1) {
        if (NULL==fgets(ln,MAXCHARS,f)) break;//


        hh++;
        L=strlen(ln)-1;
        if ('\n'!=ln[L]) {//超长行忽略后面内容
            printf("%s Line %d too long(>%d),spilth ignored.\n",argv[1],hh,MAXCHARS);
            while (1) {
                c=fgetc(f);
                if ('\n'==c 
[解决办法]
 EOF==c) break;//
            }
        }
        while (1) {//去掉行尾的'\n'和空格
            if ('\n'==ln[L] 
[解决办法]
 ' '==ln[L]) {
                ln[L]=0;
                L--;
                if (L<0) break;//
            } else break;//
        }
        if (L>=0) {
            strcpy(buf+i,ln);i+=MAXCHARS;
            n++;
            if (n>=MAXLINES) {
                MAXLINES2=MAXLINES*2;
                if (MAXLINES2==1280000) MAXLINES2=2500000;
                buf2=(char *)realloc(buf,MAXLINES2*MAXCHARS);
                if (NULL==buf2) {
                    printf("Can not malloc(%d LINES*%d CHARS)!\n",MAXLINES2,MAXCHARS);
                    printf("WARNING: Lines >%d ignored.\n",MAXLINES);
                    break;//
                }
                buf=buf2;
                MAXLINES=MAXLINES2;
            }
        }
    }
    fclose(f);
    if (n>1) {
        if (ignore_case) qsort(buf,n,MAXCHARS,icompare);
        else qsort(buf,n,MAXCHARS,compare);


    }
    f=fopen(argv[2],"w");
    if (NULL==f) {
        free(buf);
        printf("Can not create file %s!\n",argv[2]);
        return 2;
    }
    fprintf(f,"%s\n",buf);
    if (n>1) {
        if (ignore_case) {
            hh=0;
            L=MAXCHARS;
            for (i=1;i<n;i++) {
                if (stricmp((const char *)buf+hh,(const char *)buf+L)) {
                    fprintf(f,"%s\n",buf+L);
                }
                hh=L;
                L+=MAXCHARS;
            }
        } else {
            hh=0;
            L=MAXCHARS;
            for (i=1;i<n;i++) {
                if ( strcmp((const char *)buf+hh,(const char *)buf+L)) {
                    fprintf(f,"%s\n",buf+L);
                }
                hh=L;
                L+=MAXCHARS;
            }
        }
    }
    fclose(f);
    free(buf);
    return 0;
}


[解决办法]
其实LZ的第三个想法是最普遍适用的。
如果不想自己写的话可以用C++的STL库。

热点排行