How to get distinct elements from an array by avoiding duplicate elements?
package
com.blogspot.rajvishwakarma18;
public
class
MyDisticntElements {
public
static
void
printDistinctElements(
int
[] arr){
for
(
int
i=
0
;i<arr.length;i++){
boolean
isDistinct =
false
;
for
(
int
j=
0
;j<i;j++){
if
(arr[i] == arr[j]){
isDistinct =
true
;
break
;
}
}
if
(!isDistinct){
System.out.print(arr[i]+
" "
);
}
}
}
public
static
void
main(String a[]){
int
[] nums = {15,5,8,15,2,9
};
MyDisticntElements.printDistinctElements(nums);
}
}
output:15,5,8,2,9
No comments:
Post a Comment