The XYZ daycare has installed a new high-tech system for keeping track of which
ID: 3571898 • Letter: T
Question
The XYZ daycare has installed a new high-tech system for keeping track of which children are under its supervision at any moment in time. Each parent of a child has a card with a unique ID, where the ID is some integer x. Every morning, the parent swipes the card at a card-reader when dropping their child off at the daycare. The system then adds x to an array of integers. In the evening, when the parent comes to pick up the child, the same card is swiped, and a second copy of x is added to the list. Each night at midnight, the array is wiped clean and reset to have zero elements. In this way, the daycare can quickly check every afternoon which children are yet to be picked up by their parents by picking out the array elements that occur precisely once. Unfortunately, at 4:55 pm on Thursday, December 1, 2016, the daycare realizes that somewhere in their daycare is precisely one child who is yet to be picked up - can you help them determine the integer ID of this child? Requirements: 1. The file name must be named as Problem1.java 2. Write a static method as specified in the following method header public static int checkIDs(int[] idMapping) Input: The input consists of two rows. On the first row, an integer n satisfying 1<= n <= 10,000 is given. On the second row, a sequence of n integers is given. Output: The ID of the child left in the daycare. Sample Inputs 7 1 1 2 9 -11 -11 9 9 1 2 1 3 2 4 3 4 5 Sample Outputs 2 5
Explanation / Answer
Problem1.java
import java.util.Scanner;
public class Problem1 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the size of an array: ");
int n = scan.nextInt();
int idMapping[] = new int[n];
System.out.println("Array elements: ");
for(int i=0; i<n; i++){
idMapping[i] = scan.nextInt();
}
int left = checkIDs(idMapping);
System.out.println("The ID of the child left in the daycare "+left);
}
public static int checkIDs(int[] idMapping) {
int left = 0;
for(int i=0; i<idMapping.length; i++){
int count = 0;
for(int j=0; j<idMapping.length; j++){
if(idMapping[i] == idMapping[j]){
count++;
}
}
if(count == 1){
left = idMapping[i];
}
}
return left;
}
}
Output:
Enter the size of an array:
9
Array elements:
1 2 1 3 2 4 3 4 5
The ID of the child left in the daycare 5
Enter the size of an array:
7
Array elements:
1 1 2 9 -11 -11 9
The ID of the child left in the daycare 2
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.