Print duplicates and its occurrences in the array ? Solution required urgently !!! e. Print duplicates and its occurrences in the array (for ex: if array is [2,2,3,4,4,5] then your output will be as follows : Number : Appearance 2 : 2 3: 1 4 :2
Hi Deepak Kumar Singh public class FindDuplicate { public static void countDuplicate(){ List<Integer> numList = new List<Integer>{2, 2, 3, 4, 4, 5}; Map<Integer, Integer> countDuplicateMAp = new Map<Integer, Integer>(); for(Integer each : numList){ countDuplicateMAp.put(each, countNumber(each, numList)); } system.debug('countDuplicateMAp : '+ countDuplicateMAp); } public static Integer countNumber(Integer DigNum, List<Integer>numerList){ Integer countNum = 0; system.debug('DigNum : '+ DigNum);
for(Integer val : numerList){ system.debug('valu : '+ val); if(val == DigNum){ ++countNum; } } return countNum; } } I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too. Thanks and Regards, Suraj
It is the perfect time to make some plans for the future and it is time to be happy. I’ve read this post and if I could I desire to suggest you few interesting things or tips. Perhaps you could write the next articles referring to this article. I want to read more things about it!
public class thecodeclass {
public static void main(){
list<integer> listt=new list<integer>{2,3,2,2,4};
map<integer,integer> amap=new map<integer,integer>();
for(integer a:listt){
if(amap.get(a)<>null){
amap.put(a, amap.get(a)+1);}
else{
amap.put(a,1);
}
}
system.debug(amap);
}
}
All Answers
public class thecodeclass {
public static void main(){
list<integer> listt=new list<integer>{2,3,2,2,4};
map<integer,integer> amap=new map<integer,integer>();
for(integer a:listt){
if(amap.get(a)<>null){
amap.put(a, amap.get(a)+1);}
else{
amap.put(a,1);
}
}
system.debug(amap);
}
}
public class FindDuplicate {
public static void countDuplicate(){
List<Integer> numList = new List<Integer>{2, 2, 3, 4, 4, 5};
Map<Integer, Integer> countDuplicateMAp = new Map<Integer, Integer>();
for(Integer each : numList){
countDuplicateMAp.put(each, countNumber(each, numList));
}
system.debug('countDuplicateMAp : '+ countDuplicateMAp);
}
public static Integer countNumber(Integer DigNum, List<Integer>numerList){
Integer countNum = 0;
system.debug('DigNum : '+ DigNum);
for(Integer val : numerList){
system.debug('valu : '+ val);
if(val == DigNum){
++countNum;
}
}
return countNum;
}
}
I hope you find the above solution helpful.
If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Suraj
Caliburn Koko Prime Vision