[My solution C language interview series] 008 remove repeated numbers in the array

Source: Internet
Author: User
[My solution C language interview series] 008 remove repeated numbers in the array

Remove repeated numbers in the array

 

There is an array with a size of 100. The numbers in the array are between 1 and 99, but the numbers in the array are repeated. Please write a function to remove the repeated numbers in the array.

# Define init_num-1

Method 1: (the easiest way to think)

Void removebufferrepnum_00 (INT buffer [])

{

Int I, J;

 

For (I = 0; I <buffersize; I ++)

{

For (j = I + 1; j <buffersize; j ++)

{

If (buffer [I] = buffer [J])

{

Buffer [I] = init_num;

Break;

}

}

}

For (I = 0, j = 0; I <buffersize; I ++)

{

If (buffer [I] = init_num)

Continue;

Buffer [J ++] = buffer [I];

}

 

While (j <buffersize)

Buffer [J ++] = init_num;

}

This algorithm is the simplest, and the time complexity is O (n2)

 

Method 2: (use the hash table method)

Void removebufferrepnum_01 (INT buffer [])

{

Int tbuffer [buffersize];

Int I = 0, j = 0;

For (I = 0; I <buffersize; I ++) // initializes the Array

Tbuffer [I] = init_num;

 

For (I = 0; I <buffersize; I ++) // remove an algorithm

{

If (tbuffer [buffer [I] = init_num)

Tbuffer [buffer [I] = buffer [I];

}

 

For (I = 0; I <buffersize; I ++)

{

If (tbuffer [I] = init_num)

Continue;

Buffer [J ++] = tbuffer [I];

}

While (j <buffersize)

Buffer [J ++] = init_num;

}

This method is implemented by enabling the auxiliary space and setting the hash table. It can be executed n times in total. Time Complexity: O (n ). However, the only weakness is that extra space is required.

 

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.