Loading...
Loading...
Build a small hash set of students without any collection class. A Student (roll, name) has equals() and hashCode() (Objects.hash(roll, name)). The set has 16 buckets; a student goes to bucket Math.abs(hashCode() % 16). ADD puts a student in only if no equal student is already there; FIND reports whether an equal student is inside. Print the result of every command, then the size.
ADD roll name or FIND roll name.Added <name> or Duplicate <name> for ADD, Yes or No for FIND, then Size: <count>.Example
Input
6 ADD 1 Aarav ADD 2 Diya ADD 1 Aarav FIND 2 Diya FIND 2 Kavya ADD 1 Kavya
Output
Added Aarav Added Diya Duplicate Aarav Yes No Added Kavya Size: 3
A new Aarav object with the same roll and name is a duplicate; roll 1 with another name is a different student.
Try the problem first. Open one hint at a time.
Use a Student[16][n] array for the buckets and an int[16] for how many are in each.
contains looks only in the student's own bucket and compares with equals().
add calls contains first, so a duplicate never gets in.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor