Last active
August 27, 2018 02:14
-
-
Save rr-codes/d5b998c34b5a324114b12aa33e143612 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Sample input: 1 5 2 5 3 -1 -> not distinct | |
// Terminate input with a negative integer and return key | |
import java.util.*; | |
public class DuplicateCheck { | |
public static void main(String[] args) { | |
Scanner input = new Scanner(System.in); | |
boolean isDistinct = true; int num; | |
System.out.println("Enter sequence of numbers: "); | |
List<Integer> list = new ArrayList<>(); | |
do { | |
num = input.nextInt(); | |
list.add(num); | |
} while (num >= 0); | |
input.close(); | |
Set<Integer> set = new TreeSet<>(list); | |
if (set.size() < list.size()) isDistinct = false; | |
System.out.println("They are " +(isDistinct ? "" : "not ")+ "distinct" ); | |
System.out.println("Sorted elements: " + set); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment