Element removal--count garlic (9)

Source: Internet
Author: User

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)

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.