I have an array Of
String[] arr={"Google","Google","Facebook","Gmail","Twitter","Twitter"};
how to can I remove all Duplicate Elements values? this Array should be converted into
String[] arr={"Google","Facebook","Gmail","Twitter"};
- 5 years ago
you can take the help of Set collection
int[] arr={1,1,1,3,4,3,5,6,6};
int endsVal = arr.length;
Set<Integer> set = new HashSet<Integer>();
for(int i = 0; i < endsVal ; i++){
set.add(arr[i]);
}
now if you will iterate through this set, it will contain only unique values. Iterating code is like this :
Iterator itr = set.iterator();
while(itr.hasNext()) {
System.out.println(itr.next());
}
- 5 years ago
int
[] nums = {
5
,
2
,
7
,
2
,
4
,
7
,
8
,
2
,
3
};
for
(
int
i=
0;i<nums.length;i++){
boolean
isDistinct =
false
;
for
(
int
j=
0
;j<i;j++){
if(nums[i] == nums[j]){
isDistinct =
true
;
break
;
}
}
if
(!isDistinct){
System.out.print(nums[i]+" "
);
}
}
}
- 5 years ago
You can also use this
String[] arr={"Google","Google","Facebook","Gmail","Twitter","Twitter"};
List<String>new
Arr= ???????new ArrayList<String>();
for(int i=0; i<arr.length; i++){ if(Arrays.asList(new
Arr???????).contains(a
rr[i]???????)
){new
Arr.add(a
rr[i]); } }
Hot Questions