Netease 2017 intern recruitment machine questions-eliminate repetitive elements, 2017 interns

Source: Internet
Author: User

Netease 2017 intern recruitment machine questions-eliminate repetitive elements, 2017 interns

Question:

Xiaoyi has a sequence whose length is n. Xiaoyi wants to remove the repeated elements, but Xiaoyi wants to retain the last one for each element. John has encountered difficulties. I hope you can help him.

Input Description: The input includes two rows:

Length of the first behavior sequence n (1 ≤ n ≤ 50)
The second behavior is the number of n sequence [I] (1 ≤ sequence [I] ≤ 1000), separated by spaces.

Output description: The sequence after repeated elements are eliminated. The sequence is separated by spaces. There is no space at the end of the row.

Input example:

9

100 100 100 99 99 100 100 100

Output example:

99 100

 

When the subject obtained the question, he thought, well, it is not difficult to put this question in the first question. It is also very normal and has a good idea. He brushed it up in eclipse, however, I first compiled it using the HashSet method. The input and output of the sample are correct. However, after copying it to the Web debugging box, no matter how the debugging is successful, I was puzzled, because the test time is limited, someone else's code is used first. Because I was not convinced, I tried again the next day and had to pass my code, so I found the cause.

In HasSet, identical elements are not allowed, but at the bottom layer of HasSet, the elements are automatically arranged in ascending order, and the question setting requires that the last element of the same element be retained. Therefore, this is a conflict, the HashSet method is obviously not feasible, and it cannot determine the order of adding elements, so we should use the ArrayList method.

The Code is as follows:

It should be noted that, because the set is added to the element in reverse order, the elements in the set must be output in reverse order to ensure that the last identical element is output, the key is to clarify the logic, the question is not difficult, and the key is to clarify the ideas.

Import java. util. *; public class Main {public static void main (String [] args) {external scan = new external (System. in); while (scan. hasNextLine () {String num = scan. nextLine (); String str = scan. nextLine (); find (str) ;}} public static void find (String str) {String [] ch = str. split (""); List <String> list = new ArrayList <String> ();/* Add the last element to the set to ensure that the last identical element is left, pave the way for backward output */for (int I = ch. length-1; I> = 0; I --) {if (! List. contains (ch [I]) list. add (ch [I]);}/* because the set is in reverse order, the elements in the set must be output in reverse order during the output, to ensure that the output is the last identical element */for (int I = list. size ()-1; I> = 0; I --) {if (I! = 0) System. out. print (list. get (I) + ""); elseSystem. out. print (list. get (I ));}}}

  

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.