C language deletes the repeating element and time complexity in a unordered integer array

Source: Internet
Author: User

One problem, presumably, is to write a function deal to remove the repeating element in an unordered integer array (for example, int i_arr[] = {1, 2, 2, 3, 4, 2, 3, 5};) and return the final length.

1 ideas

When you see this problem, the first reaction is to delete the element and then associate it with a single linked list. But the next thought is not a good deal, because a single linked list must first traverse the elements in the array to the linked list node.

To change your mind, you can first create another integer array (the same size as the original array), it then iterates through the elements in the array, comparing the current element with all the elements before it, if the integer has not previously occurred, then putting it in the new array, and having the METHOD1 in section 2 The other is that you don't need to create a new array, when you iterate through the elements in the array, compare the current element with all the elements behind it, and if you repeat all of the following elements forward (that is, overwrite), then you have the METHOD2 in section 2.

2 Full Program

The--J statement in the 104th line of the program is important to prevent the current element from appearing 3 consecutive times (or more) without being deleted.

#include <stdio.h>
#include <string.h>
#include <malloc.h>
#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 a array for storing the elements after deleting the repeated.
int f_del1 (int *array, int ilen)
{
int i = 1;
int i_recycle = 0;
Flags to store a 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 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<i_f_del1_len; 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<len; i++)
{
For (j=i+1 j<len; 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<len; ++i)
{
PRINT ("i_f_del2[%d]=[%d].", I, i_f_del2[i]);
}
#endif

return Len;
}

3 Test Execution

Use the makefile file in the article "Common Makefile for generating elf, dynamic/static library files in Linux C + + project" To compile the program, of course, you can also compile GCC using the command int_del_repeat.c-o int_del_ Repeat

4 time complexity

The time complexity in Method 2 is O (n^2), and the time complexity in Method 2 is O (n^3).

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.