Computer Science, asked by cuteskie2276, 17 days ago

2. Duplicated ProductsYou are given a complex list of n products, eachwith a name, price, and weight. Find out how manyduplicate products are present within the list.Duplicate products contain identical parametersfor all fields in the list (i.e. name, price, andweight).​

Answers

Answered by Sarah1012
0

Iam really sorry but I didn't understand your question and from which class is this question from..?

Answered by Jasleen0599
0

Duplicated ProductsYou are given a complex list of n products, eachwith a name, price, and weight. Find out how manyduplicate products are present within the list.Duplicate products contain identical parametersfor all fields in the list (i.e. name, price, andweight).​

JAVA CODE

class Result {

   public static int numDuplicates(List<String> name, List<Integer> price, List<Integer> weight) {

   int count = 0;

   List<obj> classList = new ArrayList<obj>();

   for(int i=0; i<name.size(); i++){

       obj holder = new obj(name.get(i),price.get(i),weight.get(i));

       classList.add(holder);

   }

   for(int i=0; i<classList.size(); i++){

       for(int j=i+1; j<classList.size();j++){

           if(classList.get(i).isDuplicate(classList.get(j))){

               count++;

               classList.remove(classList.get(j));

               j--;

           }

       }

   }

   return count;

}

}

class obj{

 String name;

 int price;

 int weight;

 public obj(String name, int price, int weight){

     this.name = name;

     this.price = price;

     this.weight = weight;

 }

 public boolean isDuplicate(obj x){

     if(this.name.equals(x.name) && this.price == x.price && this.weight == x.weight){

         return true;

     }

     return false;

  }

}

  • Count the number of times a product appears in the list as a duplicate. A product that is identical to another product in every way is considered a duplicate.
  • This is the code I created, and it is O(n2), but some test cases are running out of time, thus I believe it needs an O(n) solution, but I can't think of one.

#SPJ2

Similar questions