c语言去掉数组中重复的,C语言删除无序整型数组中的重复元素及时间复杂度

数组重复的问题在任何编程中都会有碰到了,这里介绍C语言删除无序整型数组中的重复元素及时间复杂度,希望对各位有帮助。

遇到一个题,大概要求是写一个函数处理来去掉一个无序的整型数组(例如int i_arr[] = { 1, 2, 2, 3, 4, 2, 3, 5 };)中重复的元素,并返回最终的长度。

1 思路

看到这道题的时候,第一反应就是需要删除元素,然后联想到单链表。但是后面一想还是不划算,因为单链表还得先把数组中的元素遍历到链表节点中。

换一下思路,可以先创建另一个整型数组(大小和原数组一样),然后正向遍历数组中的元素,比较当前元素和它前面所有的元素是否重复,如果这个整数之前没有出现过,那么就放到新的数组中,于是有了小节2中的Method1;另外一种就是不需要创建新的数组,在正向遍历数组中的元素时,比较当前元素和它后面所有的元素是否重复,如果重复就把后面的所有元素向前移动(即覆盖),于是有了小节2中的Method2。

2 完整程序

程序中第104行的--j语句非常重要,这是为了避免当前元素连续出现3次(或以上)而没有被删除。

#include

#include

#include

#include "print.h"

int f_del1( int *i, int iLen );

int f_del2( int *i_f_del2, int len );

int main( int argc, char **argv )

{

//The test array.

int i_arr1[26] = { 1, 3, 2, 1, 2, 3, 4, 5, 5, 6, 7, 8, 12, 11, 22, 3, 7, 5, 13, 4, 5, 8, 7, 6, 23, 12 };

int i_arr2[26] = { 1, 3, 2, 1, 2, 3, 4, 5, 5, 6, 7, 8, 12, 11, 22, 3, 7, 5, 13, 4, 5, 8, 7, 6, 23, 12 };

int i_ar2r[26] = { 1, 2, 2, 3, 3, 3, 4, 5, 5, 6, 7, 8, 11, 11, 12, 13, 13, 13, 13, 14, 15, 15, 17, 18, 23, 24 };

int i_ar3r[26] = { 1, 2, 2, 3, 3, 3, 4, 5, 5, 6, 7, 8, 11, 11, 12, 13, 13, 13, 13, 14, 15, 15, 17, 18, 23, 24 };

//The length of .

int i_p_len = 0;

#if 1

i_p_len = f_del1( i_ar2r, 26 );

PRINT( "len=[%d].", i_p_len );

#endif

PRINT( "------------------------------\n" );

#if 1

i_p_len = f_del2( i_ar3r, 26 );

PRINT( "len=[%d].", i_p_len );

#endif

return 0;

}

//Method 1: Using malloc to init an array for storing the elements after deleting the repeated ones.

int f_del1( int *array, int iLen )

{

int i = 1;

int i_recycle = 0;

//Flags to store an element into the array i_f_del1.

int i_flag = 1;

//Length of the sorted array, name as i_f_del1.

int i_f_del1_len = 1;

//Init an array for storing the elements after deleting the repeated ones.

int *i_f_del1 = (int *)malloc( iLen*sizeof(int) );

//Init the first interger element.

*i_f_del1 = *array;

while( i < iLen )

{

i_flag = 1;

i_recycle = 0;

while( i_recycle < i )

{

if( array[i] == array[i_recycle ] )

{

i_flag = 0;

break;

}

}

//If i_flag equals 1, we should put the current element to the array i_f_del1.

if( i_flag )

{

i_f_del1[i_f_del1_len ] = array[i];

}

i;

}

#if 1

for( i=0; i

{

PRINT( "i_f_del1[%d]=[%d].", i, i_f_del1[i] );

}

#endif

return i_f_del1_len;

}

//Method 2: cover up the repeated elements.

int f_del2( int *i_f_del2, int len )

{

int i = 0, j = 0, k = 0;

for( i=0; i

{

for( j=i 1; j

{

if( i_f_del2[i] == i_f_del2[j] )

{

for( k=j 1; k < len; k )

{

i_f_del2[k-1] = i_f_del2[k];        //cover up

}

--len;

//Key step to avoiding the continuous elements repeated more than 2 times.

--j;

}

}

}

#if 1

for( i=0; i

{

PRINT( "i_f_del2[%d]=[%d].", i, i_f_del2[i] );

}

#endif

return len;

}

3 测试执行

使用《Linux C/C 工程中可生成ELF、动/静态库文件的通用Makefile》一文中的Makefile文件进行程序编译,当然也可以使用命令进行编译gcc int_del_repeat.c -o int_del_repeat。

4 时间复杂度

Method 2中的时间复杂度为O(N^2),Method 2中的时间复杂度为O(N^3)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值