SlideShare a Scribd company logo
Write a method rarest that accepts a map whose keys are strings and whose values are integers
as a parameter and returns the integer value that occurs the fewest times in the map. If there is a
tie, return the smaller integer value. If the map is empty, throw an exception. For example,
suppose the map contains mappings from students' names (strings) to their ages (integers). Your
method would return the least frequently occurring age. Consider a map variable m containing
the following key/value pairs: {Alyssa=22, Char=25, Dan=25, Jeff=20, Kasey=20, Kim=20,
Mognan=25, Ryan=25, Stef=22} Three people are age 20 (Jeff, Kasey, and Kim), two people
are age 22 (Alyssa and Stef), and four people are age 25 (Char, Dan, Mogran, and Ryan). So a
call of rarest (m) returns 22 because only two people are that age. If there is a tie (two or more
rarest ages that occur the same number of times), return the youngest age among them. For
example, if we added another pair of Kelly=22 to the map above, there would now be a tie of
three people of age 20 (Jeff, Kasey, Kim) and three people of age 22 (Alyssa, Kelly, Stef). So a
call of rarest(m) would now return 20 because 20 is the smaller of the rarest values. Type your
solution here: public static int rarest(Map m) {}
Solution
Code:
public static int rarest(Map m) {
int age = 0;
if (m.size() == 0)
m = null;
Collection c = m.values();
Object values[] = c.toArray();
Set set = new TreeSet();
for (Object a : values) {
set.add(a);
}
Map m2 = new HashMap();
for (Object o : set) {
m2.put(o, 0);
}
for (Object a : values) {
for (Object b : set) {
if (a.equals(b)) {
age = m2.get(b);
age++;
m2.put(b, age);
}
}
}
int n = 0;
for (Object o : set) {
if (n == 0 || m2.get(o) < n) {
n = m2.get(o);
age = (int) o;
} else {
if (m2.get(o) == n) {
if ((int) o < age) {
age = (int) o;
}
}
}
}
return age;
}

More Related Content

More from Eye2eyeopticians10

Why is it easier to classify protists by methods of nutrition and mo.pdf
Why is it easier to classify protists by methods of nutrition and mo.pdfWhy is it easier to classify protists by methods of nutrition and mo.pdf
Why is it easier to classify protists by methods of nutrition and mo.pdf
Eye2eyeopticians10
 
Why would you not expect water to move easily through a membrane Ho.pdf
Why would you not expect water to move easily through a membrane Ho.pdfWhy would you not expect water to move easily through a membrane Ho.pdf
Why would you not expect water to move easily through a membrane Ho.pdf
Eye2eyeopticians10
 
Which OSI layer is concerned with reliable end-to-end delivery of da.pdf
Which OSI layer is concerned with reliable end-to-end delivery of da.pdfWhich OSI layer is concerned with reliable end-to-end delivery of da.pdf
Which OSI layer is concerned with reliable end-to-end delivery of da.pdf
Eye2eyeopticians10
 
I need help with the 2nd TODO comment and the other comments Ill.pdf
I need help with the 2nd TODO comment and the other comments Ill.pdfI need help with the 2nd TODO comment and the other comments Ill.pdf
I need help with the 2nd TODO comment and the other comments Ill.pdf
Eye2eyeopticians10
 
How many total bits are required for a direct-mapped cache with 2048 .pdf
How many total bits are required for a direct-mapped cache with 2048 .pdfHow many total bits are required for a direct-mapped cache with 2048 .pdf
How many total bits are required for a direct-mapped cache with 2048 .pdf
Eye2eyeopticians10
 
Which of the following statements is (are) trueI The budget is .pdf
Which of the following statements is (are) trueI  The budget is .pdfWhich of the following statements is (are) trueI  The budget is .pdf
Which of the following statements is (are) trueI The budget is .pdf
Eye2eyeopticians10
 
Which of the following is the most appropriate way to determine how .pdf
Which of the following is the most appropriate way to determine how .pdfWhich of the following is the most appropriate way to determine how .pdf
Which of the following is the most appropriate way to determine how .pdf
Eye2eyeopticians10
 
Which of the following is a rational number A. pi B. 1.425 C. squ.pdf
Which of the following is a rational number  A. pi  B. 1.425  C. squ.pdfWhich of the following is a rational number  A. pi  B. 1.425  C. squ.pdf
Which of the following is a rational number A. pi B. 1.425 C. squ.pdf
Eye2eyeopticians10
 
Where is a thyme corpuscle found specifically sinus capsule medulla .pdf
Where is a thyme corpuscle found specifically sinus capsule medulla .pdfWhere is a thyme corpuscle found specifically sinus capsule medulla .pdf
Where is a thyme corpuscle found specifically sinus capsule medulla .pdf
Eye2eyeopticians10
 
what are the 3 mechanisms by which RTKs can activate signaling enzym.pdf
what are the 3 mechanisms by which RTKs can activate signaling enzym.pdfwhat are the 3 mechanisms by which RTKs can activate signaling enzym.pdf
what are the 3 mechanisms by which RTKs can activate signaling enzym.pdf
Eye2eyeopticians10
 
This figure displays five small hypothetical proteins. The a-helix se.pdf
This figure displays five small hypothetical proteins. The a-helix se.pdfThis figure displays five small hypothetical proteins. The a-helix se.pdf
This figure displays five small hypothetical proteins. The a-helix se.pdf
Eye2eyeopticians10
 
Solve the following initial value problem using Laplace transform.pdf
Solve the following initial value problem using Laplace  transform.pdfSolve the following initial value problem using Laplace  transform.pdf
Solve the following initial value problem using Laplace transform.pdf
Eye2eyeopticians10
 
Supplemental Problem Phases in precipitation hardening For precipita.pdf
Supplemental Problem Phases in precipitation hardening For precipita.pdfSupplemental Problem Phases in precipitation hardening For precipita.pdf
Supplemental Problem Phases in precipitation hardening For precipita.pdf
Eye2eyeopticians10
 

More from Eye2eyeopticians10 (13)

Why is it easier to classify protists by methods of nutrition and mo.pdf
Why is it easier to classify protists by methods of nutrition and mo.pdfWhy is it easier to classify protists by methods of nutrition and mo.pdf
Why is it easier to classify protists by methods of nutrition and mo.pdf
 
Why would you not expect water to move easily through a membrane Ho.pdf
Why would you not expect water to move easily through a membrane Ho.pdfWhy would you not expect water to move easily through a membrane Ho.pdf
Why would you not expect water to move easily through a membrane Ho.pdf
 
Which OSI layer is concerned with reliable end-to-end delivery of da.pdf
Which OSI layer is concerned with reliable end-to-end delivery of da.pdfWhich OSI layer is concerned with reliable end-to-end delivery of da.pdf
Which OSI layer is concerned with reliable end-to-end delivery of da.pdf
 
I need help with the 2nd TODO comment and the other comments Ill.pdf
I need help with the 2nd TODO comment and the other comments Ill.pdfI need help with the 2nd TODO comment and the other comments Ill.pdf
I need help with the 2nd TODO comment and the other comments Ill.pdf
 
How many total bits are required for a direct-mapped cache with 2048 .pdf
How many total bits are required for a direct-mapped cache with 2048 .pdfHow many total bits are required for a direct-mapped cache with 2048 .pdf
How many total bits are required for a direct-mapped cache with 2048 .pdf
 
Which of the following statements is (are) trueI The budget is .pdf
Which of the following statements is (are) trueI  The budget is .pdfWhich of the following statements is (are) trueI  The budget is .pdf
Which of the following statements is (are) trueI The budget is .pdf
 
Which of the following is the most appropriate way to determine how .pdf
Which of the following is the most appropriate way to determine how .pdfWhich of the following is the most appropriate way to determine how .pdf
Which of the following is the most appropriate way to determine how .pdf
 
Which of the following is a rational number A. pi B. 1.425 C. squ.pdf
Which of the following is a rational number  A. pi  B. 1.425  C. squ.pdfWhich of the following is a rational number  A. pi  B. 1.425  C. squ.pdf
Which of the following is a rational number A. pi B. 1.425 C. squ.pdf
 
Where is a thyme corpuscle found specifically sinus capsule medulla .pdf
Where is a thyme corpuscle found specifically sinus capsule medulla .pdfWhere is a thyme corpuscle found specifically sinus capsule medulla .pdf
Where is a thyme corpuscle found specifically sinus capsule medulla .pdf
 
what are the 3 mechanisms by which RTKs can activate signaling enzym.pdf
what are the 3 mechanisms by which RTKs can activate signaling enzym.pdfwhat are the 3 mechanisms by which RTKs can activate signaling enzym.pdf
what are the 3 mechanisms by which RTKs can activate signaling enzym.pdf
 
This figure displays five small hypothetical proteins. The a-helix se.pdf
This figure displays five small hypothetical proteins. The a-helix se.pdfThis figure displays five small hypothetical proteins. The a-helix se.pdf
This figure displays five small hypothetical proteins. The a-helix se.pdf
 
Solve the following initial value problem using Laplace transform.pdf
Solve the following initial value problem using Laplace  transform.pdfSolve the following initial value problem using Laplace  transform.pdf
Solve the following initial value problem using Laplace transform.pdf
 
Supplemental Problem Phases in precipitation hardening For precipita.pdf
Supplemental Problem Phases in precipitation hardening For precipita.pdfSupplemental Problem Phases in precipitation hardening For precipita.pdf
Supplemental Problem Phases in precipitation hardening For precipita.pdf
 

Recently uploaded

Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
CarlosHernanMontoyab2
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 

Recently uploaded (20)

Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 

Write a method rarest that accepts a map whose keys are strings and w.pdf

  • 1. Write a method rarest that accepts a map whose keys are strings and whose values are integers as a parameter and returns the integer value that occurs the fewest times in the map. If there is a tie, return the smaller integer value. If the map is empty, throw an exception. For example, suppose the map contains mappings from students' names (strings) to their ages (integers). Your method would return the least frequently occurring age. Consider a map variable m containing the following key/value pairs: {Alyssa=22, Char=25, Dan=25, Jeff=20, Kasey=20, Kim=20, Mognan=25, Ryan=25, Stef=22} Three people are age 20 (Jeff, Kasey, and Kim), two people are age 22 (Alyssa and Stef), and four people are age 25 (Char, Dan, Mogran, and Ryan). So a call of rarest (m) returns 22 because only two people are that age. If there is a tie (two or more rarest ages that occur the same number of times), return the youngest age among them. For example, if we added another pair of Kelly=22 to the map above, there would now be a tie of three people of age 20 (Jeff, Kasey, Kim) and three people of age 22 (Alyssa, Kelly, Stef). So a call of rarest(m) would now return 20 because 20 is the smaller of the rarest values. Type your solution here: public static int rarest(Map m) {} Solution Code: public static int rarest(Map m) { int age = 0; if (m.size() == 0) m = null; Collection c = m.values(); Object values[] = c.toArray(); Set set = new TreeSet(); for (Object a : values) { set.add(a); } Map m2 = new HashMap(); for (Object o : set) { m2.put(o, 0); } for (Object a : values) { for (Object b : set) { if (a.equals(b)) { age = m2.get(b);
  • 2. age++; m2.put(b, age); } } } int n = 0; for (Object o : set) { if (n == 0 || m2.get(o) < n) { n = m2.get(o); age = (int) o; } else { if (m2.get(o) == n) { if ((int) o < age) { age = (int) o; } } } } return age; }