0% found this document useful (0 votes)
65 views1 page

Java Program for Counting Integers

This document contains code for a program that counts the frequency of integers in an input. It uses an ArrayList to store the integers and a HashMap to count frequencies. The program iterates through the input n times, adding each integer to the ArrayList and HashMap. It then iterates through the ArrayList again to print each integer and its frequency before setting the frequency to null. The overall time complexity of the algorithm is O(n).

Uploaded by

Milena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views1 page

Java Program for Counting Integers

This document contains code for a program that counts the frequency of integers in an input. It uses an ArrayList to store the integers and a HashMap to count frequencies. The program iterates through the input n times, adding each integer to the ArrayList and HashMap. It then iterates through the ArrayList again to print each integer and its frequency before setting the frequency to null. The overall time complexity of the algorithm is O(n).

Uploaded by

Milena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Nombre: JI

Fecha: 24/abril/2018

Materia: Taller de programación

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class uva_484 {


public static void main(String[] args){ // 1
Scanner lee = new Scanner(System.in); // 1
ArrayList<Integer> arr = new ArrayList<>(); // 1
Map<Integer,Integer> hm = new HashMap<Integer, Integer>(); // 1
while(lee.hasNext()) { // n + 1
int c = lee.nextInt(); // n
if(hm.containsKey(c)) // n
hm.put(c, hm.get(c)+1); // n
else
hm.put(c, 1); // n

arr.add(c); // n
}
for(int i = 0; i < arr.size(); i++){ // n + 1
int key = arr.get(i); // n

if(hm.get( key ) != null){ // n


System.out.println(key + " " + hm.get(key)); // n
hm.put(key, null); // n
} ---------------
} T(n) = 11n +6
}
} T(n) ∈ O(n)

You might also like