首页 > 试题广场 >

消除重复元素

[编程题]消除重复元素
  • 热度指数:5411 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
小易有一个长度为n序列,小易想移除掉里面的重复元素,但是小易想是对于每种元素保留最后出现的那个。小易遇到了困难,希望你来帮助他。

输入描述:
输入包括两行: 第一行为序列长度n(1 ≤ n ≤ 50) 第二行为n个数sequence[i](1 ≤ sequence[i] ≤ 1000),以空格分隔


输出描述:
输出消除重复元素之后的序列,以空格分隔,行末无空格
示例1

输入

9 100 100 100 99 99 99 100 100 100

输出

99 100
//在加入list之前先做判断,如果没有出现过,则加入末尾,如果出现过,从list中删除这个数,在加入末尾
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * Created by meichen on 17-9-7.
 */
public class Main {
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        while (in.hasNext()){
            int num = in.nextInt();
            int[] A = new int[num];
            for(int i = 0; i < num; i++){
                A[i] = in.nextInt();
            }
            List<Integer> list = new ArrayList<Integer>();
            for(int n : A){
                if(!list.contains(n)){
                    list.add(n);
                }else {
                    list.remove((Object)(n));
                    list.add(n);
                }
            }
            String str = "";
            for(int i = 0; i < list.size() - 1; i++){
                str += list.get(i) + " ";
            }
            str += list.get(list.size() - 1);
            System.out.print(str);
        }
    }
}


发表于 2017-09-07 18:52:32 回复(0)
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Scanner;

/**
*使用一个双端队列即可,比较简单,直接show the code
关键代码就五行
        Deque<Integer> q = new ArrayDeque<>();
        for (int i = n - 1; i >= 0; i --) {
            if (!q.contains(sequence[i])) {
                q.offerFirst(sequence[i]);
            }
        }
*/
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n  = sc.nextInt();
        int[] sequence = new int[n];
        for (int i = 0; i < n; i ++) {
            sequence[i] = sc.nextInt();
        }
        Deque<Integer> q = new ArrayDeque<>();
        for (int i = n - 1; i >= 0; i --) {
            if (!q.contains(sequence[i])) {
                q.offerFirst(sequence[i]);
            }
        }
        int count = q.size();
        for (int i = 0; i < count - 1; i ++) {
            System.out.print(q.poll() + " ");
        }
        System.out.println(q.poll());
    }
}


发表于 2017-04-09 10:36:58 回复(0)

import java.util.*;

/**

  • Created by yahto on 01/04/2017. */ public class Main { public static void main(String[] args){
     Scanner scanner = new Scanner(System.in);
     List<Integer> list = new ArrayList<Integer>();
     List<Integer> list1 = new ArrayList<Integer>();
     while (scanner.hasNext()){
         Integer count = scanner.nextInt();
         for (int i = 0; i < count; i++) {
             Integer num = scanner.nextInt();
             list.add(num);
         }
         for (int i = 0; i < count; i++) {
             Integer temp = list.remove(0);
             if (!list.contains(temp)){
                 list1.add(temp);
             }
         }
         for (int i = 0; i < list1.size() - 1; i++) {
             System.out.print(list1.get(i) + " ");
         }
         System.out.println(list1.get(list1.size()-1));
     }
     scanner.close();
    
    } }
发表于 2017-04-01 15:07:13 回复(0)
import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner scan=new Scanner(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>();		

		for(int i=ch.length-1;i>=0;i--)  
			{
				if(!list.contains(ch[i]))
					list.add(ch[i]);		
			}

		for(int i=list.size()-1;i>=0;i--) 
		{
			if(i!=0)
				System.out.print(list.get(i)+" ");
			else
				System.out.print(list.get(i));
		}
	}
}



发表于 2017-03-27 16:13:36 回复(0)