11th question: Remove repeated elements from the array and remove the Array

Source: Internet
Author: User

11th question: Remove repeated elements from the array and remove the Array
/* I climbed to Taishan a few days ago, but I haven't updated it all the time. I still don't feel much about it when I went up the hill. I am about to crash after going down the hill. My bones are scattered and I will continue to update my bones ......*/Question: Remove repeated elements from the array

Given an array in ascending order, remove the number of duplicates and return the length of the new array.

For example:

Array A = {1, 1, 2}, your function should return length 2, new array is {1, 2}

Requirements:

You cannot create an array to allocate additional space. That is, the constant space limit.

Tip:

Input an integer n and its corresponding array A [n], and output the length of the new array.

Sample Input

50 0 1 1 2

Sample output

3

Resolution:
# Include <stdio. h> int main () {/* I is the cyclic variable, n and A [] are the requirements of the question, temp is A temporary value, compare the stored values with A [I + 1]. If they are equal, leave the next value empty. Otherwise, assign A [I + 1] value to tempcount as the number of null values, subtracting the number of null values from the original array is the new array length */int I, n, A [1000], temp, count = 0; scanf ("% d ", & n); for (I = 0; I <n; I ++) {scanf ("% d", & A [I]);} temp = A [0]; for (I = 1; I <n; I ++) {if (A [I] = temp) {A [I] = ''; count ++;} elsetemp = A [I];} printf (" % d \ n ", n-count); return 0 ;}

If you cannot understand it, please leave a message or leave an email !!! O (partition _ partition) o

C language, delete repeated elements in the array

# Include <stdio. h>
# Define N 1000
# Define FLAG 0x80000000/* special mark, expressed with the smallest negative number */
Void main ()
{
Int r [N], length, I, j;
Scanf ("% d", & length );
For (I = 0; I <length; I ++)
Scanf ("% d", & r [I]);
For (I = 0; I <length-1; I ++)
{
If (r [I]! = FLAG)
{
For (j = I + 1; j <length; j ++)
If (r [j] = r [I]) r [j] = FLAG;/* overwrites a duplicate value with a special mark */
}
}
For (I = 0; r [I]! = FLAG; I ++);/* Find the first special mark */
For (j = I + 1; j <length;)/* Delete the special mark in the sequence */
{
If (r [j]! = FLAG) r [I ++] = r [j ++];
Else j ++;
}
Length = I;/* modify the length of the sequence after the repeated values are deleted */
For (I = 0; I <length; I ++)
Printf ("%-4d", r [I]);
}

Delete repeated elements in an array from a two-dimensional array in C Language

You can use a one-dimensional array to calculate duplicate and non-repeated elements.
# Include <stdio. h>
Void main ()
{
Int I, j, k;
Int a [50];
Int B [50], c [50];
Int n, flag = 0;
For (I = 1; I <50; I ++) c [I] = 0;
Printf ("Please enter 50 data \ n ");
For (I = 0; I <50; I ++) scanf ("% d", & a [I]);
N = 0;
B [n] = a [0]; c [n] = 1; n ++;
For (I = 1; I <50; I ++ ){
For (k = 0; k <n; k ++ ){
If (a [I] = B [k]) {c [k] = c [k] + 1; flag = 1; break ;}
}
If (flag = 0) {B [n] = a [I]; c [n] = 1; n ++;} else {flag = 0 ;};
}
For (I = 0; I <50; I ++ ){
If (c [I] = 1) printf ("% d", B [I]);
}
Printf ("\ n =================\ n ");
For (I = 0; I <50; I ++ ){
If (c [I]> 1) printf ("% d -- % d \ n", B [I], c [I]);
}
}

--------------------
"Deleting repeated elements in an array" does not know what it means.
Array units are continuous allocation units. It is difficult to delete even dynamically allocated units.

Dynamic Allocation of units, you can use realloc to increase the allocation unit and reduce the allocation unit. However, it can only start from the end and the unit is continuous.

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.