Find distinct elements Java
PROGRAM TO PRINT ALL DISTINCT ELEMENTS OF A GIVEN INTEGER ARRAY
OUTPUT
10 5 3 4 6
import
java.util.*;
class
Main
{
// This function prints all distinct elements
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] +
" "
);
}
}
}
// Driver method to test above method
public
static
void
main (String[] args)
{
int
arr[] = {
10
,
5
,
3
,
4
,
3
,
5
,
6
};
printDistinct(arr);
}
10 5 3 4 6
Comments
Post a Comment