6-1 Duplicated Words (20分)

java旧题复习
6-1 Duplicated Words (20分)
This program reads a lot of words, in which may be duplicated words. The program picks out all the duplicated ones and sorts the remainders in a descendent order.

函数接口定义:
public static ArrayList pick(ArrayList a);
a is the ArrayList to be parsed and returns the result as an ArrayList.

Sample referee test procedure:

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Scanner;

public class Main {
    
    
    /* 请在这里填写答案 */

    public static void main(String[] args) {
    
    
        ArrayList<String> lst = new ArrayList<>();
        Scanner in = new Scanner(System.in);
        while ( in.hasNext() ) {
    
    
            lst.add(in.next());
        }
        lst = pick(lst);
        for ( String x:lst) {
    
    
            System.out.print(x+" ");
        }
        System.out.println();
        in.close();
    }
}

Input sample:

this is a test at this Zhejiang University

Sample output:

this test is at a Zhejiang University 

years:

public static ArrayList<String> pick(ArrayList<String> a){
    
    
		ArrayList<String> list=new ArrayList<>();
		
		for(int i=0;i<a.size();i++) {
    
    
			if(list.contains(a.get(i))==false) {
    
    
				list.add(a.get(i));
			}
		}
		Collections.sort(list);
		Collections.reverse(list);
		return list;
	}

Guess you like

Origin blog.csdn.net/timelessx_x/article/details/111914449