· 6 years ago · Feb 15, 2020, 02:08 AM
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
9work.
10*/
11
12
13import java.util.Arrays;
14import java.util.LinkedHashMap;
15import java.util.Map;
16
17public 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 }