Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
ShowAll Questionssorted byDate Posted
ramesh

# 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

Ankaiah (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);```

Thanks!!​​​​​​​

Ankaiah (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);```

Thanks!!

ramesh
Getting error elCount What is elcount
Ankaiah (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);```