SlideShare a Scribd company logo
1 of 4
Download to read offline
Hi please find my code.
import java.util.HashMap;
import java.util.Map;
public class Max3Bag {
private HashMap map;
public Max3Bag() {
map = new HashMap<>();
}
public void add(String element){
if(map.containsKey(element)){ // if map already contains this element
if(map.get(element) < 3) // if count is less than 3 then add this element with count
incremented by 1
map.put(element, map.get(element)+1);
}else
map.put(element, 1); // if element is not in map then add it it map with count 1
}
public void remove(String element){
if(map.containsKey(element)){// if element is in map
if(map.get(element) == 1) // if count is 1 then remove from map
map.remove(element);
else // else decrement count by 1
map.put(element, map.get(element)-1);
}
}
public boolean member(String element){
return map.containsKey(element);
}
public void print(){
// get each entry of map and print key and value
for(Map.Entry entry : map.entrySet()){
System.out.println(entry.getKey()+"="+entry.getValue());
}
}
}
public class Max3BagTest {
public static void main(String[] args) {
Max3Bag A = new Max3Bag();
A.add("a");
A.add("a");
A.add("a");
A.add("a");
A.add("b");
A.add("b");
A.add("b");
A.remove("a");
A.add("c");
A.print();
}
}
/*
Sample Output:
a=2
b=3
c=1
*/
Solution
Hi please find my code.
import java.util.HashMap;
import java.util.Map;
public class Max3Bag {
private HashMap map;
public Max3Bag() {
map = new HashMap<>();
}
public void add(String element){
if(map.containsKey(element)){ // if map already contains this element
if(map.get(element) < 3) // if count is less than 3 then add this element with count
incremented by 1
map.put(element, map.get(element)+1);
}else
map.put(element, 1); // if element is not in map then add it it map with count 1
}
public void remove(String element){
if(map.containsKey(element)){// if element is in map
if(map.get(element) == 1) // if count is 1 then remove from map
map.remove(element);
else // else decrement count by 1
map.put(element, map.get(element)-1);
}
}
public boolean member(String element){
return map.containsKey(element);
}
public void print(){
// get each entry of map and print key and value
for(Map.Entry entry : map.entrySet()){
System.out.println(entry.getKey()+"="+entry.getValue());
}
}
}
public class Max3BagTest {
public static void main(String[] args) {
Max3Bag A = new Max3Bag();
A.add("a");
A.add("a");
A.add("a");
A.add("a");
A.add("b");
A.add("b");
A.add("b");
A.remove("a");
A.add("c");
A.print();
}
}
/*
Sample Output:
a=2
b=3
c=1
*/

More Related Content

More from aptind

Starting with Main.java, where I tested everythingimport College..pdf
Starting with Main.java, where I tested everythingimport College..pdfStarting with Main.java, where I tested everythingimport College..pdf
Starting with Main.java, where I tested everythingimport College..pdfaptind
 
solution of question no.6inputPresent stateNext stateoutput.pdf
solution of question no.6inputPresent stateNext stateoutput.pdfsolution of question no.6inputPresent stateNext stateoutput.pdf
solution of question no.6inputPresent stateNext stateoutput.pdfaptind
 
Sexual reproduction has played the most crucial role in evolution of.pdf
Sexual reproduction has played the most crucial role in evolution of.pdfSexual reproduction has played the most crucial role in evolution of.pdf
Sexual reproduction has played the most crucial role in evolution of.pdfaptind
 
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdfpackage com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdfaptind
 
And is option DIf variable interest rate decrease , asset value wi.pdf
And is option DIf variable interest rate decrease , asset value wi.pdfAnd is option DIf variable interest rate decrease , asset value wi.pdf
And is option DIf variable interest rate decrease , asset value wi.pdfaptind
 
import java.util.Scanner;public class Factorial { method usi.pdf
import java.util.Scanner;public class Factorial { method usi.pdfimport java.util.Scanner;public class Factorial { method usi.pdf
import java.util.Scanner;public class Factorial { method usi.pdfaptind
 
Given below is the code for the question. Since the test files (ment.pdf
Given below is the code for the question. Since the test files (ment.pdfGiven below is the code for the question. Since the test files (ment.pdf
Given below is the code for the question. Since the test files (ment.pdfaptind
 
Cisco Systems, Inc Acquisition Integration for manufacturing at.pdf
Cisco Systems, Inc Acquisition Integration for manufacturing at.pdfCisco Systems, Inc Acquisition Integration for manufacturing at.pdf
Cisco Systems, Inc Acquisition Integration for manufacturing at.pdfaptind
 
As we understand, when soil particles binds to each other more stron.pdf
As we understand, when soil particles binds to each other more stron.pdfAs we understand, when soil particles binds to each other more stron.pdf
As we understand, when soil particles binds to each other more stron.pdfaptind
 
Amount deposited (base amount) = 2000Rate of interest = 5Amount.pdf
Amount deposited (base amount) = 2000Rate of interest = 5Amount.pdfAmount deposited (base amount) = 2000Rate of interest = 5Amount.pdf
Amount deposited (base amount) = 2000Rate of interest = 5Amount.pdfaptind
 
24. Accomodation - n. Ability of lens to chhange shape diminishes as.pdf
24. Accomodation - n. Ability of lens to chhange shape diminishes as.pdf24. Accomodation - n. Ability of lens to chhange shape diminishes as.pdf
24. Accomodation - n. Ability of lens to chhange shape diminishes as.pdfaptind
 
1.They trade away higher fecundity for future reproduction.2.Resou.pdf
1.They trade away higher fecundity for future reproduction.2.Resou.pdf1.They trade away higher fecundity for future reproduction.2.Resou.pdf
1.They trade away higher fecundity for future reproduction.2.Resou.pdfaptind
 
A character device typically transfers data to and from a user appli.pdf
A character device typically transfers data to and from a user appli.pdfA character device typically transfers data to and from a user appli.pdf
A character device typically transfers data to and from a user appli.pdfaptind
 
FALSSolutionFALS.pdf
FALSSolutionFALS.pdfFALSSolutionFALS.pdf
FALSSolutionFALS.pdfaptind
 
1. ingestion-placement of food into mouth.2.propulsion-peristalsis.pdf
1. ingestion-placement of food into mouth.2.propulsion-peristalsis.pdf1. ingestion-placement of food into mouth.2.propulsion-peristalsis.pdf
1. ingestion-placement of food into mouth.2.propulsion-peristalsis.pdfaptind
 
1)Coupling-   It is applicable on different elements of a service.pdf
1)Coupling-   It is applicable on different elements of a service.pdf1)Coupling-   It is applicable on different elements of a service.pdf
1)Coupling-   It is applicable on different elements of a service.pdfaptind
 
#include iostream     Provides cout. #include cstdlib   .pdf
 #include iostream      Provides cout. #include cstdlib   .pdf #include iostream      Provides cout. #include cstdlib   .pdf
#include iostream     Provides cout. #include cstdlib   .pdfaptind
 
ultravoilet exites electrons of matter more than .pdf
                     ultravoilet exites electrons of matter more than .pdf                     ultravoilet exites electrons of matter more than .pdf
ultravoilet exites electrons of matter more than .pdfaptind
 
The difference in the HNMR is the number of signa.pdf
                     The difference in the HNMR is the number of signa.pdf                     The difference in the HNMR is the number of signa.pdf
The difference in the HNMR is the number of signa.pdfaptind
 
The balanced Equations are as follows 2SO2(g) +.pdf
                     The balanced Equations are as follows  2SO2(g) +.pdf                     The balanced Equations are as follows  2SO2(g) +.pdf
The balanced Equations are as follows 2SO2(g) +.pdfaptind
 

More from aptind (20)

Starting with Main.java, where I tested everythingimport College..pdf
Starting with Main.java, where I tested everythingimport College..pdfStarting with Main.java, where I tested everythingimport College..pdf
Starting with Main.java, where I tested everythingimport College..pdf
 
solution of question no.6inputPresent stateNext stateoutput.pdf
solution of question no.6inputPresent stateNext stateoutput.pdfsolution of question no.6inputPresent stateNext stateoutput.pdf
solution of question no.6inputPresent stateNext stateoutput.pdf
 
Sexual reproduction has played the most crucial role in evolution of.pdf
Sexual reproduction has played the most crucial role in evolution of.pdfSexual reproduction has played the most crucial role in evolution of.pdf
Sexual reproduction has played the most crucial role in evolution of.pdf
 
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdfpackage com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
 
And is option DIf variable interest rate decrease , asset value wi.pdf
And is option DIf variable interest rate decrease , asset value wi.pdfAnd is option DIf variable interest rate decrease , asset value wi.pdf
And is option DIf variable interest rate decrease , asset value wi.pdf
 
import java.util.Scanner;public class Factorial { method usi.pdf
import java.util.Scanner;public class Factorial { method usi.pdfimport java.util.Scanner;public class Factorial { method usi.pdf
import java.util.Scanner;public class Factorial { method usi.pdf
 
Given below is the code for the question. Since the test files (ment.pdf
Given below is the code for the question. Since the test files (ment.pdfGiven below is the code for the question. Since the test files (ment.pdf
Given below is the code for the question. Since the test files (ment.pdf
 
Cisco Systems, Inc Acquisition Integration for manufacturing at.pdf
Cisco Systems, Inc Acquisition Integration for manufacturing at.pdfCisco Systems, Inc Acquisition Integration for manufacturing at.pdf
Cisco Systems, Inc Acquisition Integration for manufacturing at.pdf
 
As we understand, when soil particles binds to each other more stron.pdf
As we understand, when soil particles binds to each other more stron.pdfAs we understand, when soil particles binds to each other more stron.pdf
As we understand, when soil particles binds to each other more stron.pdf
 
Amount deposited (base amount) = 2000Rate of interest = 5Amount.pdf
Amount deposited (base amount) = 2000Rate of interest = 5Amount.pdfAmount deposited (base amount) = 2000Rate of interest = 5Amount.pdf
Amount deposited (base amount) = 2000Rate of interest = 5Amount.pdf
 
24. Accomodation - n. Ability of lens to chhange shape diminishes as.pdf
24. Accomodation - n. Ability of lens to chhange shape diminishes as.pdf24. Accomodation - n. Ability of lens to chhange shape diminishes as.pdf
24. Accomodation - n. Ability of lens to chhange shape diminishes as.pdf
 
1.They trade away higher fecundity for future reproduction.2.Resou.pdf
1.They trade away higher fecundity for future reproduction.2.Resou.pdf1.They trade away higher fecundity for future reproduction.2.Resou.pdf
1.They trade away higher fecundity for future reproduction.2.Resou.pdf
 
A character device typically transfers data to and from a user appli.pdf
A character device typically transfers data to and from a user appli.pdfA character device typically transfers data to and from a user appli.pdf
A character device typically transfers data to and from a user appli.pdf
 
FALSSolutionFALS.pdf
FALSSolutionFALS.pdfFALSSolutionFALS.pdf
FALSSolutionFALS.pdf
 
1. ingestion-placement of food into mouth.2.propulsion-peristalsis.pdf
1. ingestion-placement of food into mouth.2.propulsion-peristalsis.pdf1. ingestion-placement of food into mouth.2.propulsion-peristalsis.pdf
1. ingestion-placement of food into mouth.2.propulsion-peristalsis.pdf
 
1)Coupling-   It is applicable on different elements of a service.pdf
1)Coupling-   It is applicable on different elements of a service.pdf1)Coupling-   It is applicable on different elements of a service.pdf
1)Coupling-   It is applicable on different elements of a service.pdf
 
#include iostream     Provides cout. #include cstdlib   .pdf
 #include iostream      Provides cout. #include cstdlib   .pdf #include iostream      Provides cout. #include cstdlib   .pdf
#include iostream     Provides cout. #include cstdlib   .pdf
 
ultravoilet exites electrons of matter more than .pdf
                     ultravoilet exites electrons of matter more than .pdf                     ultravoilet exites electrons of matter more than .pdf
ultravoilet exites electrons of matter more than .pdf
 
The difference in the HNMR is the number of signa.pdf
                     The difference in the HNMR is the number of signa.pdf                     The difference in the HNMR is the number of signa.pdf
The difference in the HNMR is the number of signa.pdf
 
The balanced Equations are as follows 2SO2(g) +.pdf
                     The balanced Equations are as follows  2SO2(g) +.pdf                     The balanced Equations are as follows  2SO2(g) +.pdf
The balanced Equations are as follows 2SO2(g) +.pdf
 

Recently uploaded

Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 

Recently uploaded (20)

Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 

Hi please find my code.import java.util.HashMap;import java.util.pdf

  • 1. Hi please find my code. import java.util.HashMap; import java.util.Map; public class Max3Bag { private HashMap map; public Max3Bag() { map = new HashMap<>(); } public void add(String element){ if(map.containsKey(element)){ // if map already contains this element if(map.get(element) < 3) // if count is less than 3 then add this element with count incremented by 1 map.put(element, map.get(element)+1); }else map.put(element, 1); // if element is not in map then add it it map with count 1 } public void remove(String element){ if(map.containsKey(element)){// if element is in map if(map.get(element) == 1) // if count is 1 then remove from map map.remove(element); else // else decrement count by 1 map.put(element, map.get(element)-1); } } public boolean member(String element){ return map.containsKey(element); } public void print(){ // get each entry of map and print key and value
  • 2. for(Map.Entry entry : map.entrySet()){ System.out.println(entry.getKey()+"="+entry.getValue()); } } } public class Max3BagTest { public static void main(String[] args) { Max3Bag A = new Max3Bag(); A.add("a"); A.add("a"); A.add("a"); A.add("a"); A.add("b"); A.add("b"); A.add("b"); A.remove("a"); A.add("c"); A.print(); } } /* Sample Output: a=2 b=3 c=1 */ Solution Hi please find my code.
  • 3. import java.util.HashMap; import java.util.Map; public class Max3Bag { private HashMap map; public Max3Bag() { map = new HashMap<>(); } public void add(String element){ if(map.containsKey(element)){ // if map already contains this element if(map.get(element) < 3) // if count is less than 3 then add this element with count incremented by 1 map.put(element, map.get(element)+1); }else map.put(element, 1); // if element is not in map then add it it map with count 1 } public void remove(String element){ if(map.containsKey(element)){// if element is in map if(map.get(element) == 1) // if count is 1 then remove from map map.remove(element); else // else decrement count by 1 map.put(element, map.get(element)-1); } } public boolean member(String element){ return map.containsKey(element); } public void print(){ // get each entry of map and print key and value for(Map.Entry entry : map.entrySet()){ System.out.println(entry.getKey()+"="+entry.getValue());
  • 4. } } } public class Max3BagTest { public static void main(String[] args) { Max3Bag A = new Max3Bag(); A.add("a"); A.add("a"); A.add("a"); A.add("a"); A.add("b"); A.add("b"); A.add("b"); A.remove("a"); A.add("c"); A.print(); } } /* Sample Output: a=2 b=3 c=1 */