This method is within a class implementing a Linked List Th

This method is within a class implementing a Linked List.

       // This method creates and returns a new set from the items contained in the elements array.
       // The array may contain duplicates, but the Set implementation should not contain duplicates.
       // If the array is empty or null, return a set of size 0 with head referring to null.
       // If duplicates of an item are present in the array, then ignore all the occurrences of this
       // item after the first one.

public ISet fromArray(Object[] elements) {

       return null;
   }

Solution

hi, I had implemented the logic of, how to perform the above mentioned task. But since you didn\'t added any information about the Item class, I can\'t write that code

But, I had written the code that will definetly help you with your code :

import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class Test {

   public static void main (String[] args) throws java.lang.Exception
   {
       int []array={1,5,22,7,3,10,23,3,5,10,1};
       Test t=new Test();
       t.fromArray(array);
   }
  
   public Set fromArray(int[] array)
   {
       Set<Integer> set=new HashSet<Integer>();
     
       for(int i : array)
       {
           if(!set.contains(i))
           {
               set.add(i);
           }
       }
       return set;
    }
}


here, I had declared the Set of Integer type, you need to add as Set<Item> set=new HashSet<Item>();
so that your set contains the object of Item type only.

Second thing is, while looping through the array, you need to check for item object in the set. Dont worry about the comparsions & search. Set will take care of it itself as it has its own algorithms & comparators

feel free to ask if you have any doubt :)

This method is within a class implementing a Linked List. // This method creates and returns a new set from the items contained in the elements array. // The ar

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site