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
rameshramesh 

Apex task: 1,2,3,4,3,2,1 you have to print how many unique numbers are in this list and how many times it is repeated in this list

Best Answer chosen by ramesh
AnkaiahAnkaiah (Salesforce Developers) 
Hi Saki,

try with below.
List<integer> intvales = new List<integer>{1,2,3,4,3,2,1};

set<integer> uniquevalues = new set<integer>(intvales);
system.debug('uniquevalues=='+uniquevalues);
Map<integer,Integer> RepeatedCount = new Map<integer,Integer>();

for(integer key : intvales)
{
    if(!RepeatedCount.containsKey(key)){
    RepeatedCount.put(key,0);
    }
    Integer currentInt=RepeatedCount .get(key)+1;
    RepeatedCount.put(key,currentInt);
}

system.debug('Repeated Values count=='+RepeatedCount);

If this helps, Please mark it as best answer.

Thanks!!​​​​​​​

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi Saki,

try with below in devconsole.
List<integer> intvales = new List<integer>{1,2,3,4,3,2,1};

set<integer> uniquevalues = new set<integer>(intvales);
system.debug('uniquevalues=='+uniquevalues);
Map<integer,Integer> RepeatedCount = new Map<integer,Integer>();

for(integer key : intvales)
{
    if(!RepeatedCount.containsKey(key)){
    RepeatedCount.put(key,0);
    }
    Integer currentInt=elCount.get(key)+1;
    RepeatedCount.put(key,currentInt);
}

system.debug('Repeated Values count=='+RepeatedCount);

If this helps, Please mark it as best answer.

Thanks!!
 
rameshramesh
Getting error elCount What is elcount
AnkaiahAnkaiah (Salesforce Developers) 
Hi Saki,

try with below.
List<integer> intvales = new List<integer>{1,2,3,4,3,2,1};

set<integer> uniquevalues = new set<integer>(intvales);
system.debug('uniquevalues=='+uniquevalues);
Map<integer,Integer> RepeatedCount = new Map<integer,Integer>();

for(integer key : intvales)
{
    if(!RepeatedCount.containsKey(key)){
    RepeatedCount.put(key,0);
    }
    Integer currentInt=RepeatedCount .get(key)+1;
    RepeatedCount.put(key,currentInt);
}

system.debug('Repeated Values count=='+RepeatedCount);

If this helps, Please mark it as best answer.

Thanks!!​​​​​​​
This was selected as the best answer