(IN JAVA) 3 ZIPS, Any Duplicates? Write a program named ZipDuplicates that reads
ID: 3727667 • Letter: #
Question
(IN JAVA) 3 ZIPS, Any Duplicates? Write a program named ZipDuplicates that reads in three zip codes from standard input and writes to standard output a single integer and nothing else. The integer it prints out is the number of duplicates in the sequence of zip codes. Thus, it will print out 0, 1 or 2: 0 is printed if each zip code is unique. 1 is printed if only two of the zip codes are the same. 2 is printed if all three are the same. So, if the input to the program is 11210 11217 10003 then the output would be: ------------------------------------ 0 ------------------------------------ But, if the input to the program is 11210 11210 11210 then the output would be: ------------------------------------ 2 ------------------------------------ CONSTRAINT: Use a method to determine and return the number of duplicates.
Explanation / Answer
Solution
import java.util.*;
public class MyClass {
public static void main(String args[]) {
Scanner scn=new Scanner(System.in);
String input=scn.nextLine();
String[] arr=input.split(" ");
int ans=findDuplicates(arr);
System.out.println("------------------------ "+ans+" --------------------");
}
public static int findDuplicates(String[] arr){
if(arr[0].equals(arr[1]) && arr[1].equals(arr[2]))
return 2;
if(arr[0].equals(arr[1]) || arr[1].equals(arr[2]) || arr[0].equals(arr[2]))
return 1;
return 0;
}
}
sample input/output:
input:
11210 11217 10003
output:
------------------------ 0 --------------------
input:
11210 11210 11210
output
------------------------ 2 --------------------
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.