(UVA) The Department of Redundancy Department - Solution

Link to the problem: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=425

A map structure is used to keep the number as the key and the amount of times that this number appeared as the value. 


import java.io.*;
import java.util.*;

class Main {
    public void process() throws NumberFormatException, IOException {
        Scanner sc = new Scanner(System.in);
        
        HashMap<Integer, Integer> map = new HashMap<>();
        ArrayList<Integer> order = new ArrayList<>();
        
        while (sc.hasNext()) {
            int number = sc.nextInt();
            if (!map.containsKey(number)) {
                map.put(number, 0);
                order.add(number);
            }        
            map.put(number, map.get(number)+1);
        }
        
        for (int i = 0; i < order.size(); i++) {
            System.out.println(order.get(i) + " " + map.get(order.get(i)));
        }
        
        return;
    }
    
    public static void main(String[] args) throws NumberFormatException, IOException {
        Main m = new Main();
        m.process();
        
        System.exit(0);
    }
}

Comments

Popular posts from this blog

(Coderbyte) Powers of Two - Solução

(Coderbyte) Dash Insert II - Solução

(CoderByte) Number Search - Solução