This program takes n
integers in the list
and then gives the count
of the next t
numbers entered by the user. If the numbers are in the list it prints the count
and if it is not then it gives the "NOT PRESENT"
message. I want to know if I can reduce its time and space
complexity or do I have to take another approach for the program.
import java.util.*;
class Memorise_Me
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
List<Integer> list=new ArrayList<Integer>(n);
while(n>0)
{
list.add(sc.nextInt());
n--;
}
int t=sc.nextInt();
int x,count=0;
while(t>0)
{
x=sc.nextInt();
count=Collections.frequency(list,x);
if(count==0)
System.out.println("NOT PRESENT");
else
System.out.println(count);
t--;
}
sc.close();
}