Skip to main content
グループ

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!

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 件の回答
  1. 9月2日 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 件のコメント
  1. 8月31日 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 件の回答
0/9000

#17 - Age Group

public String ageGroup(Integer n) {

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

}

2 件のコメント
0/9000

Hey,  

Check out how I implemented problem 55 - Companion plants 2: 

public Boolean companionPlants(List<String> plants) { 

    //code here 

    if(plants.size()==1){ 

        return false; 

    } 

 

    Map<String, List<String>> companionMap = new Map<String, List<String>>(); 

 

    companionMap.put('lettuce', new List<String>{'cucumbers', 'onions'});  

    companionMap.put('onions', new List<String>{'lettuce', 'carrots', 'tomatoes'}); 

    companionMap.put('cucumbers', new List<String>{'lettuce'}); 

    companionMap.put('carrots', new List<String>{'onions'}); 

    companionMap.put('tomatoes', new List<String>{'onions'}); 

 

    for(Integer i = 0; i < plants.size()-1; i++){ 

 

        List<String> currentCompanions = companionMap.get(plants[i]); 

 

        if(!currentCompanions.contains(plants[i+1])){ 

 

            return false; 

 

        } 

 

    } 

 

 

 

    return true; 

 

 

Now I am in top 4 percent of all ApexSandbox users.

0/9000

 

I've just completed Apex 101 and Database 101 challenges :) . This positions me in the top 8 percent of ApexSandbox users.

I've just completed Apex 101 and Database 101 challenges :) . This positions me in the top 8 percent of ApexSandbox users.

 

 

0/9000