Skip to main content
Gruppe

Apex Practice Problems - ApexSandbox.io

View and post solutions to programming problems on www.apexsandbox.io, ask questions and get help from a community of people who love to code!

#118 - Find Common Characters

 

public List<String> commonChars(List<String> strs){

//solution here

Integer[] listCount = new Integer[26];

for (Integer i = 0; i < 26; i++)

listCount[i] = 2147483647;

for (String s : strs){

Integer[] wordCount = new Integer[26];

for (Integer i = 0; i < 26; i++)

wordCount[i] = 0;

for (Integer i = 0; i < s.length(); i++){

wordCount[s.charAt(i) - 97] = wordCount[s.charAt(i) - 97] + 1;

}

System.debug(wordCount);

for (Integer i = 0; i < 26; i++)

listCount[i] = Math.min(listCount[i], wordCount[i]);

}

System.debug(listCount);

List<String> res = new List<String>();

for (Integer i = 0; i < 26; i++){

Integer count = listCount[i];

while (count > 0){

count--;

String myChar = String.fromCharArray(new List<integer>{ i + 97 });

res.add(myChar);

}

}

return res;

}

4 Kommentare
0/9000

#87 - Cases by Type

public Map<String, List<Case>> casesByType(List<Case> cases) {

Map<String, List<Case>> result = new Map<String, List<Case>>();

for (Case c : cases) {

if (c.Type != null) {

if (result.containsKey(c.Type)) {

result.get(c.Type).add(c);

}

else {

result.put(c.Type, new List<Case> { c });

}

}

}

return result;

}

1 Kommentar
  1. 9. Sept., 23:51

    public static Map<String, List<Case>> casesByType(List<Case> cases) {

     

     Map<String, List<Case>> result = new Map<String, List<Case>>();

     

     for (Case c : cases) {

     if (String.isBlank(c.Type)) {

     continue;

     }

     

    result.put(c.Type, (result.get(c.Type) ?? new List<Case>()));

    result.get(c.Type).add(c);

     }

     

    return result;

    }

0/9000

Posting the solution here as I couldn't find one for this problem

 

public static Integer findLast(List<Integer> nums, Integer target) {

    Integer index;

    if(!nums.contains(target))

    {

        return -1;

    }

    for(Integer i = nums.size()-1; i >= 0; i--)

    {

        if(nums[i] == target)

        {

            index = i;

            break;

        }

    }

    return index;

3 Antworten
  1. 2. Sept., 22:42

    //alternate solution 

    for(Integer i = nums.size() - 1; i >= 0; i--) 

    if(nums[i] == target) return i;

    return -1;

0/9000

#80 - Duplicate Integers

public Boolean containsDuplicates(List<Integer> nums) {

Set<Integer> seen = new Set<Integer>();

for (Integer i=0; i<nums.size(); i++) {

if (seen.contains(nums[i])) return true;

seen.add(nums[i]);

}

return false;

}

4 Kommentare
  1. 31. Aug., 22:34

    Abdul Basith - Acknowledging and thanks for brief code.

    I want to shorten it further: 

    #92 - Sorting a List

     

    public List<String> getNamesInAscOrder(List<String> accountNames)

    {

        accountNames?.sort();

        return accountNames ?? accountNames;

     

    }

0/9000

Desenvolvedor Júnior (exclusiva para pessoas autistas)

 

A Specialisterne, em parceria com uma multinacional de referência em soluções digitais, está com inscrições abertas para pessoas autistas que queiram ingressar no mercado de trabalho.

 

As vagas são direcionadas para:

  •  Pessoas autistas com 18 anos ou mais.
  •  Com conhecimentos e/ou formação na área de desenvolvimento de software.

 

Atividades:

  • Projetar e construir soluções avançadas dentro da plataforma.
  • Garantir que os sistemas sejam escaláveis, tenham alto desempenho e se integrem de forma eficiente com outras plataformas.
  • Será parte ativa do processo de code review.
  • Seguir os fluxos de trabalho já estabelecidos pela equipe para gestão de código e participação nos processos de deployment.

 

Local de Trabalho: Zona Sul - São Paulo

 

Requisitos básicos:

  • Conhecimento em Java.
  • Noções de programação orientada a objetos e lógica de programação.
  • Interesse e motivação para aprender novas tecnologias.

 

Diferenciais (Desejáveis):

  • Apex.
  • Lightning Web Components.
  • Integrações de APIs (REST/SOAP).
  • SOQL e/ou SOSL .
  • Git para controle de versões.
  • Conhecimento de inglês.

 

A contratação será diretamente pela empresa parceira Salesforce.

 

Se você tem mais de 18 anos, atende aos requisitos informados e quer participar desse processo, preencha o formulário de candidatura a seguir:

Formulário de Inscrição

 

Desenvolvedor Júnior (exclusiva para pessoas autistas) A Specialisterne, em parceria com uma multinacional de referência em soluções digitais, está com inscrições abertas para pessoas autistas que que

 

 

0/9000

Reverse Words In String

public static String reverseWords(String str){

// solution here

List<String> revStr = new List<String>();

for(String sp : str.split(' ')){

revStr.add(sp.reverse());

}

return String.join(revStr,' ');

}

0/9000

Binary Search Opportunites

public static Integer search(List<Opportunity> opportunities, Integer target){

// solution here

Integer left = 0;

Integer right = opportunities.size();

while(left < right){

Integer middle = Math.abs((left + right) / 2);

if(opportunities[middle]. Amount == target)

return middle;

if(opportunities[middle].Amount < target){

left = middle + 1;

}else{

right = middle - 1;

}

}

return - 1;

}

0/9000

#120 Merge Two Sorted Lists

public static List<Integer> mergeLists(List<Integer> list1, List<Integer> list2){

// solution

List<Integer> finalLst = new List<Integer>();

Integer maxLen = list1.size() + list2.size();

Integer i = 0;

Integer j = 0;

Integer x = 0;

while(x < maxLen){

if(i > list1.size() - 1){

finalLst.add(list2[j]);

j++;

}else if(j > list2.size() - 1){

finalLst.add(list1[i]);

i++;

}else if(list1[i] < list2[j]){

finalLst.add(list1[i]);

i++;

}else{

finalLst.add(list2[j]);

j++;

}

x++;

}

return finalLst;

}

0/9000

One of my favorite algorithms.

 

public static Integer search(List<Opportunity> opportunities, Integer target){

Integer first = 0;

Integer last = opportunities.size()-1;

while (first <= last) {

Integer middle = first + (last - first) / 2;

if (target == opportunities[middle].Amount)

return middle;

else if (target < opportunities[middle].Amount)

last = middle - 1;

else

first = middle + 1;

}

return -1;

}

1 Antwort
0/9000

#17 - Age Group

public String ageGroup(Integer n) {

return n < 2 ? 'Infant' : n < 15 ? 'Child' : n < 22 ? 'Youth' : 'Adult';

}

2 Kommentare
0/9000