Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

public class FreqAndRankContainer { ArrayList<String> a = new ArrayList<String>(

ID: 3874189 • Letter: P

Question

public class FreqAndRankContainer {

ArrayList<String> a = new ArrayList<String>();

   String[] items;          

   int size;

   // constructor

   public FreqAndRankContainer() {  

   }

  

   // add a new copy of "item" to the container

   void add(String item) {      

       a.add(item);

   }

   // Removes one copy of "item" from the container.

   // Return true iff the item was removed. Returns false

   // if the item was not in the container to begin with.

   boolean remove(String item) {

       for(int i = 0; i < size; i++) {

           if(items[i].equals(item)) {

               items[i] = items[size - 1];

               items[size - 1] = null;

               size--;

               return true;

           }

       }

       return false;

   }

   // Returns the total number of items in the container.

   int getSize() {

       return a.size();

   }

   // Returns the number copies of "item" in the container.

   int getFrequency(String item) {

       int count = 0;

       for(int i = 0; i < size; i++) {

           if(item.equals(items[i])) {

               count ++;

           }

       }

       return count;  

   }

   // Returns the item that has the most occurrences. If a tie,

   // returns the one alphabetically first. If none, return null.

   String getMostFrequentItem() {

       return null;

   }

   // Returns the ith value in the container, where the values

   String getIthRank(int i) {

       return null;

      

   }

}

Explanation / Answer

package TestCircle;

import java.util.ArrayList;

import java.util.Collections;

public class FreqAndRankContainer {

ArrayList<String> a = new ArrayList<String>();

String[] items;

int size;

// constructor

public FreqAndRankContainer() {

}

  

// add a new copy of "item" to the container

void add(String item) {

a.add(item);

}

// Removes one copy of "item" from the container.

// Return true iff the item was removed. Returns false

// if the item was not in the container to begin with.

boolean remove(String item) {

for(int i = 0; i < size; i++) {

if(items[i].equals(item)) {

items[i] = items[size - 1];

items[size - 1] = null;

size--;

return true;

}

}

return false;

}

// Returns the total number of items in the container.

int getSize() {

return a.size();

}

// Returns the number copies of "item" in the container.

int getFrequency(String item) {

int count = 0;

for(int i = 0; i < size; i++) {

if(item.equals(items[i])) {

count ++;

}

}

return count;

}

// Returns the item that has the most occurrences. If a tie,

// returns the one alphabetically first. If none, return null.

String getMostFrequentItem() {

      

   if(a.size()==0)

       return null;

   Collections.sort(a);

   int count = 0, max = Integer.MIN_VALUE;

   String str = null;

   for(int i= 0 ;i<a.size()-1;++i){

       if(a.get(i)==a.get(i+1))

           count = count + 1;

       else

           count = 0;

          

       if(count > max){

           str = a.get(i);

       }

   }

      

   if(count > max){

           str = a.get(a.size()-1);

       }

return str;

}

// Returns the ith value in the container, where the values

String getIthRank(int i) {

   Collections.sort(a);

      

   if(a.size()==0)

       return null;

return a.get(i);

  

}

}