NetEase 2017 Spring recruit written test real problem programming problem set--4. Eliminate duplicate elements __ NetEase

Source: Internet
Author: User

java-annotation NetEase 2017 spring recruit written test real problem programming problem set--4. Eliminate duplicate element topics:

Small easy to have a length of n sequence, small easy to remove the inside of the repeating elements, but small easy to think is for each element to retain the last occurrence of that. Small easy to meet difficulties, I hope you to help him.

The original title address is entered in this description:

The input includes two lines:
First behavior sequence length n (1≤n≤50)
Second behavior n number sequence[i] (1≤sequence[i]≤1000), separated by a space output description:

Output the sequence after the elimination of duplicate elements, separated by a space, the end of the line without space input example:

9
Examples of output from the following:

is written in Java with the idea that using the scanner receive console input difficulty should be the one that is required to retain the last occurrence of each element:
as follows: 100 100 99 11 11 1 1, removing duplicate elements is necessarily: 100 99 11 And if this happens: 100 100 100 99 99 99 100 100 100, to satisfy the requirement that the last occurrence for each element is retained, three 99 before and after 100, then 99 100; just from the back traversal of the given array satisfies the requirement that the last occurrence of each element is retained, with a ArrayList to save the collection after the merged repeating element, and to determine whether the element already exists in the collection, each traversing an element. is added to the collection if it does not exist. Traversal end to reverse the set output, because the traversal of the target array is traversed from the back forward, the order to join the collection is also from the back, and the output should be previous output. Code:

Import java.util.ArrayList;
Import java.util.List;
Import Java.util.Scanner;

public class Main {public

    static void Main (string[] args) {
        Scanner Scanner = new Scanner (system.in);
        int count = Scanner.nextint ();
        Scanner.nextline ();
        String sou = Scanner.nextline ();
        string[] STRs = Sou.split ("");
        int[] Nums = new Int[count];
        int i = 0;
        for (String s:strs) {
            nums[i++] = Integer.parseint (s);
        }

        list<integer> list = new arraylist<> ();
        i = 0;
        for (int j = nums.length-1 J >= 0; j--) {
            if (!list.contains (nums[j))
                List.add (i++, nums[j]);
        For

        (int j = list.size ()-1; J >= 0; j--) {
            String out = j = = 0? list.get (j) + "": List.get (j) + ""; 
  system.out.print (out);}}
submit run prompt through all test cases:

Note that the Boolean contains (Object O) method that invokes ArrayList determines whether an element in the collection already contains a simplified code

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.