Summary of methods for deleting array elements in C Language

Source: Internet
Author: User

This article introduces how to delete array elements in C language. Here, an example of how to delete elements in a single array, single array, and two-dimensional array is provided. For more information, see.

An array with 10 elements 1 3 3 2 5 1 1 5 6 6, after deleting the same element, it becomes 1 3 2 5 6. Please help me, it is best to give all programs. It must be the delete method!

Example

The Code is as follows: Copy code

Void delete_listi (Node list, int I)
{
Node p = list; q;
Int j;/* set the auxiliary cyclic variable */
If (I = 1)
List = list-> next;
Else
{
Q = p; p = p-> next;
For (j = 2; j <I; j ++)/* Note that j starts from 2 */
{
Q = q-> next; p = p-> next;/* Move pointer */
}
Q-> next = p-> next; free (p);/* Find and release */
}
Return;
}

Example 2

The Code is as follows: Copy code

/* I wrote two methods !!! Method 1 */

# Include <stdio. h>
Int main ()
{
Int a [10] = {1, 3, 2, 5, 1, 5, 6 };
Int I, j, k, x = 10;/* x indicates the number of no repeated valid elements in the array */
For (I = 0; I <x; I ++)
{
For (j = I + 1; j <x; j ++)
{
If (a [I] = a [j])
{
X --; // subtract 1 If duplicate values are found
For (k = j; k <x; k ++) // move the Array
{
A [k] = a [k + 1];
}
J --;
}
}
}
For (I = 0; I <x; I ++) printf ("% d", a [I]);
Printf ("n ");
Return 0;
}

/*************************************** ***************************************
* Write another method !!!
* This method identifies all repeated elements and generates a new array without repeated elements.
**************************************** ***************************************/

# Include <stdio. h>
# Define flag-1 // an identifier is also defined. Assign a unique element-free value to this identifier.
Int main ()
{
Int a [10] = {1, 3, 2, 5, 1, 5, 6 };
Int B [10]; // It is used to store arrays with or without repeated elements.
Int I, j, x = 10; // x indicates the number of valid elements that are not repeated.
For (I = 0; I <10; I ++)
{
If (a [I]! = Flag) // If the logo has passed, it will not be compared.
{
For (j = I + 1; j <10; j ++)
{
If (a [I] = a [j] & a [j]! = Flag)
{
A [j] = flag;
X --; // identifies a repeated item, effectively reducing one
}
}
}
};
For (I = 0, j = 0; I <10; I ++) // returns an unidentified element to array B [].
{
If (a [I]! = Flag)
{
B [j] = a [I];
J ++;
}
};
For (I = 0; I <x; I ++) printf ("% d", B [I]);
Printf ("n ");
Return 0;
}


Delete repeated elements in the array

A defined array contains repeated elements! For example, the given array is: int a [] = {1, 2, 3, 4, 5, 3, 2, 1, 5}, and the result is: a [] = {1, 2, 3, 4, 5}; instead of defining int B [50], use "B [j] = a [I]; "transfer the elements of array a to array B!

The Code is as follows: Copy code

# Include <stdio. h>
Int main ()
{
Char str [100];
Int I = 0, j;
Printf ("input a string n ");
Gets (str );
While (I <strlen (str ))
{J = I + 1;
Printf ("delelte result % dn", strlen (str ));
While (j <strlen (str ))
{If (str [j] = str [I])
{
Str [j] = '';
Strcat (str, str + j + 1 );
}
Else j ++;
}
I ++;

}
Puts (str );

}


Delete duplicate elements in an array from a two-dimensional array

 

The Code is as follows: Copy code
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 datan ");
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 -- % dn", 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.

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.