Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Student Information
- * -------------------
- * Student Name: Patel, Ritesh
- * Student Number: 400184627
- * Course Code: CS/SE 2XB3
- * Lab Section: 03
- *
- * I attest that the following code being submitted is my own individual
- work.
- */
- import java.util.Arrays;
- import java.util.LinkedHashMap;
- import java.util.Map;
- public class Set implements API {
- private final String[] setOfStrings;
- public Set(String[] setOfStrings){
- //create LinkedHashMap to place all the strings in the array and since Maps cannot have duplicate keys
- //I use this to remove the duplicates and update the array therefore everytime the constructor is called
- //any duplicate strings are removed
- LinkedHashMap<String,Integer> result = new LinkedHashMap<>();
- for(int i = 0; i < setOfStrings.length; i++){
- result.put(setOfStrings[i],i);
- }
- String[] newSet = new String[result.size()];
- int index = 0;
- for(Map.Entry<String,Integer> key : result.entrySet()){
- newSet[index] = key.getKey();
- index ++;
- }
- this.setOfStrings = newSet;
- }
- public boolean isEmpty() {
- //if length of array is zero then it is empty
- return setOfStrings.length == 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement