Print All Distinct Elements of a given integer array - GeeksQuiz
Given an integer array, print all distinct elements in array. The given array may contain duplicates and the output should print every element only once. The given array is not sorted.
One traverse:
Read full article from Print All Distinct Elements of a given integer array - GeeksQuiz
Given an integer array, print all distinct elements in array. The given array may contain duplicates and the output should print every element only once. The given array is not sorted.
One traverse:
static void printDistinct(int arr[]) { // Creates an empty hashset HashSet<Integer> set = new HashSet<>(); // Traverse the input array for (int i=0; i<arr.length; i++) { // If not present, then put it in hashtable and print it if (!set.contains(arr[i])) { set.add(arr[i]); System.out.print(arr[i] + " "); } } }