Skip to content

Instantly share code, notes, and snippets.

@rr-codes
Last active August 27, 2018 02:14
Show Gist options
  • Save rr-codes/d5b998c34b5a324114b12aa33e143612 to your computer and use it in GitHub Desktop.
Save rr-codes/d5b998c34b5a324114b12aa33e143612 to your computer and use it in GitHub Desktop.
// 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