1. Title Description
Given an array and a number (the number is not necessarily in the array), delete the number from the array and return the remaining array length.
such as: a[] = {1, 2, 3, 4, 5}, to remove the number 3, the returned array length is 4.
Dear little Friends, is the topic very simple?
hint: int removeelement (int a[], int n, int elem)
where n represents the length of the array, and Elem represents the element to be deleted.
Format:
Enter a number n, enter an array a[n], and then enter the element elem to delete, returning the remaining array length index.
Example 1
Input:
23 33
Output:
2. Algorithmic thinking
Think of the elements in the array (collection) as the key of the table, and the number of occurrences is imagined as value.
When reading, the element being read into is the subscript of the array, and the corresponding element value in the array is added one.
For example, an array (collection) contains elements {1, 2, 2, 3, 4}. Then the corresponding Kazakhstan table is
Key |
Value |
1 |
1 |
2 |
2 |
3 |
1 |
4 |
1 |
Finally, the length of the output Array (collection) minus the number of occurrences of an element.
3. Code implementation
#include <stdio.h>#defineArrlen 100intMain () {inthashTable [Arrlen] = {0}; intI, Len, index; scanf ("%d\n", &Len); for(i =0; i < Len; i++) {scanf ("%d\n", &index); Hashtable[index]++; } scanf ("%d\n", &index); printf ("%d\n", Len-Hashtable[index]); return 0;}
Element removal--count garlic (9)