Salesforce.com Interview Question
144 Interview Reviews |
Back to all Salesforce.com Interview Questions & Reviews
Interview questions and reviews posted anonymously by interview candidates
Interview Question for Software Quality Engineer at Salesforce.com:
Give an array of Integer which contain duplicate number from 1-100, how to count how many distinct number you have?
See more for this Salesforce.com Software Quality Engineer Interview
Helpful Question?
Yes |
No
Inappropriate?
Answers & Comments (3)
0 of 1 people found this helpful
//Improved code
Set<Integer> distinctSet = new HashSet<Integer>();
for(int i = 0; i<array.length; i++){
if(!(distinctSet.contains(array[i]))){
distinctSet.add(array[i]);
}
else{
distinctSet.remove(array[i]);
}
}
System.out.println(distinctSet.size());
Helpful Answer?
Yes |
No
Inappropriate?
the answer should return 9 instead of 7,
cuz you want to find the distinct number,
this hashset should have 1,2,3,4,6,7,8,9,0,
which have 9 elements inside
Helpful Answer?
Yes |
No
Inappropriate?
To comment on this
question,
Sign In with Facebook or
Sign Up
1 of 2 people found this helpful
by Interview Candidate:
for (Integer x: inputArray){
hs.add(x);
}
s.o.p(hs.size());