function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Deepak Kumar Singh 15Deepak Kumar Singh 15 

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

Best Answer chosen by Deepak Kumar Singh 15
charchit nirayanwalcharchit nirayanwal
use map 
 

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

charchit nirayanwalcharchit nirayanwal
use map 
 

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);
    }
}
This was selected as the best answer
Suraj Tripathi 47Suraj Tripathi 47
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
Priya Singh 118Priya Singh 118
Check Here Latest Government jobs (https://vacancysquare.com/) & Government schemes information only on Vacancysquare.com 
 
GreyGorilla VaporGreyGorilla Vapor
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!

Caliburn Koko Prime Vision