SHARE
TWEET

Untitled

a guest Feb 14th, 2020 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Student Information
  2. * -------------------
  3. * Student Name: Patel, Ritesh
  4. * Student Number: 400184627
  5. * Course Code: CS/SE 2XB3
  6. * Lab Section: 03
  7. *
  8. * I attest that the following code being submitted is my own individual
  9. work.
  10. */
  11.  
  12.  
  13. import java.util.Arrays;
  14. import java.util.LinkedHashMap;
  15. import java.util.Map;
  16.  
  17. public class Set implements API {
  18.     private final String[] setOfStrings;
  19.  
  20.     public Set(String[] setOfStrings){
  21.        
  22.         //create LinkedHashMap to place all the strings in the array and since Maps cannot have duplicate keys
  23.         //I use this to remove the duplicates and update the array therefore everytime the constructor is called
  24.         //any duplicate strings are removed
  25.        
  26.         LinkedHashMap<String,Integer> result = new LinkedHashMap<>();
  27.  
  28.         for(int i = 0; i < setOfStrings.length; i++){
  29.             result.put(setOfStrings[i],i);
  30.         }
  31.  
  32.         String[] newSet = new String[result.size()];
  33.  
  34.         int index = 0;
  35.         for(Map.Entry<String,Integer> key : result.entrySet()){
  36.             newSet[index] = key.getKey();
  37.             index ++;
  38.         }
  39.  
  40.  
  41.         this.setOfStrings = newSet;
  42.  
  43.         }
  44.  
  45.     public boolean isEmpty() {
  46.         //if length of array is zero then it is empty
  47.         return setOfStrings.length == 0;
  48.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top