ArrayList Operation | Practice | GeeksforGeeks

Given an ArrayList of N lowercase characters. The task is to count the frequency of elements in the list.
Note: use add() to append element in the list and contains() to check an element is present or not in the list and Collections.frequency() to find the frequency of the element in the list.
Input Format:
The first line of the test case contains T, the number of test cases. For each test case, the first line contains the number of queries Q. Each query may be any one of the two types:
1. "i" with "c" : insert the element "c" into the ArrayList
2. "f" with "c": find the frequency of "c" in the ArrayList. 
Output Format:
For each test case, output the frequency of elements. Print "Not Present" if the element is not present in the list else its frequency in a new line for every query.
Your Task:
Your task is to complete the functions insert() and freq() which are used to insert and find the frequency of elements respectively.
Constraints:
1 <= T <= 100
1 <= N <= 105
1 <= Q <= 102
Example:
Input:
2
6
i g i e i e i k i s f e
4
i c i p i p f f
Output:
2
Not Present
Explanation:
Testcase 1: Inserting g, e, e, k, s into the list. The frequency of e is 2 in the list.
Testcase 2: Inserting c, p, p into the list. The frequency of f is 0 in the list.
0
joydebroy1120003 months ago
JAVA
 
 
 
 public static void insert(ArrayList<Character> clist, char c)   {       clist.add(c);       //add c to clist   }      // Function to count frequency of element   public static void freq(ArrayList<Character> clist, char c)   {           if(/*Your code here*/ clist.contains(c))       System.out.println(/*Your code here*/Collections.frequency(clist, c));       else       System.out.println("Not Present");          }
0
hharshit81183 months ago
Time Complexity: O(1)
class Geeks{   public static void insert(ArrayList<Character> clist, char c)   {       clist.add(c);   }      public static void freq(ArrayList<Character> clist, char c)   {           if(clist.contains(c))       System.out.println(Collections.frequency(clist,c));       else       System.out.println("Not Present");          }}
0
sanketsupekar6 months ago
class Geeks
{
    // Function to insert element
    public static void insert(ArrayList<Character> clist, char c)
    {
        clist.add(c);
    }
    public static void freq(ArrayList<Character> clist, char c)
    {
        if(clist.contains(c))
        System.out.println(Collections.frequency(clist,c));
        else
        System.out.println("Not Present");
        
    }
}
0
ADITI GAUTAM9 months ago
ADITI GAUTAM
JAVA SOLUTION: EXECUTION TIME 0.4 sclass Geeks{    // Function to insert element    public static void insert(ArrayList<character> clist, char c)    {
        clist.add(c);    }
    // Function to count frequency of element    public static void freq(ArrayList<character> clist, char c)    {
        if(clist.contains(c))
        System.out.println(Collections.frequency(clist,c));        else        System.out.println("Not Present");
    }}
0
Debojyoti Sinha1 year ago
Debojyoti Sinha
Correct Answer.Correct AnswerExecution Time:0.47
class Geeks{    public static void insert(ArrayList<character> clist, char c)    {        clist.add(c);    }        public static void freq(ArrayList<character> clist, char c)    {        if(clist.contains(c))        {            System.out.println(Collections.frequency(clist, c));        }        else        {            System.out.println("Not Present");        }    }}
0
Gaurav Sharma1 year ago
Gaurav Sharma
JAVA CODE :)
class Geeks{    // Function to insert element    public static void insert(ArrayList<character> clist, char c)    {        clist.add(c);
    }
    // Function to count frequency of element    public static void freq(ArrayList<character> clist, char c)    {
        if(clist.contains(c))        System.out.println(Collections.frequency(clist,c) );        else        System.out.println("Not Present");
    }}
0
Amar Prakash1 year ago
Amar Prakash
class Geeks{    // Function to insert element    public static void insert(ArrayList<character> clist, char c)    {        clist.add(c);        //add c to clist    }
    // Function to count frequency of element    public static void freq(ArrayList<character> clist, char c)    {
        if(clist.contains(c))        System.out.println( Collections.frequency(clist,c));        else        System.out.println("Not Present");
    }}
0
Drigger2 years ago
Drigger
Uppercase 'C' in Collections.frequency();
0
sneha.rao2 years ago
sneha.rao
*C++ code*#include <iostream>using namespace std;int main() {    int t;    cin>>t;    int n;
        while(t--)    {            cin>>n;        char ch1,ch2;        char arr[100];         int i=0, count=0;        for(int k=0;k<n;k++){ cin="">>ch1;        cin>>ch2;
        if(ch1=='i')        {            arr[i]=ch2;            i++;        }
        else{            arr[i]='\0';            int j=0;            while(arr[j]!='\0')            {                if(arr[j]==ch2)                {                 count++;                }                j++;            }            if(count==0)            {                cout<<"Not Present"<<endl; }="" else="" {="" cout<<count<<endl;="" }="" }="" }}="" return="" 0;="" }="">
0
Bharti Swami2 years ago
Bharti Swami
class Geeks{    // Function to insert element    public static void insert(ArrayList<character> clist, char c)    {
        clist.add(c);    }
    // Function to count frequency of element    public static void freq(ArrayList<character> clist, char c)    {
        if(clist.contains(c))        System.out.println( Collections.frequency(clist,c));        else        System.out.println("Not Present");
    }}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.