Yesoda Sanka 6 gadus atpakaļ
vecāks
revīzija
bf686b3381

+ 123
- 1
src/main/java/com/zipcodewilmington/looplabs/IntegerDuplicateDeleter.java Parādīt failu

@@ -1,8 +1,130 @@
1 1
 package com.zipcodewilmington.looplabs;
2 2
 
3
+import java.util.*;
4
+
3 5
 /**
4 6
  * Created by leon on 1/29/18.
5 7
  * @ATTENTION_TO_STUDENTS You are forbidden from modifying the signature of this class.
8
+ *  Given an object, `IntegerDuplicateDeleter`, with a composite `Integer[]` object, write a method
9
+ *         * `removeDuplicatesExactly` which removes all values in the array which occur exactly
10
+ *         the specified number of times.
11
+ *         * `removeDuplicates` which removes all values in the array which occur
12
+ *         at least the specified number of times.
6 13
  */
7 14
 public final class IntegerDuplicateDeleter extends DuplicateDeleter<Integer> {
8
-}
15
+    ArrayList<Integer> list = new ArrayList<Integer>();
16
+
17
+    public IntegerDuplicateDeleter(Integer[] intArray) {
18
+        super(intArray);
19
+    }
20
+
21
+   @Override
22
+    public Integer[] removeDuplicates(int maxNumberOfDuplications) {
23
+        Arrays.sort (array);
24
+       int n = array.length;
25
+       int count = 0;
26
+       int num=0;
27
+       ArrayList<Integer>arraylist=new ArrayList<>() ;
28
+       //Get all unique values
29
+       HashSet set = new HashSet();
30
+       for (int i=0; i < array.length;i++) {
31
+           set.add(new Integer(array[i]));
32
+       }
33
+
34
+       Map<Integer,Integer>map=new HashMap<>();
35
+       Iterator it = set.iterator();
36
+       // Loop through the set for duplicates count and add it to Map
37
+       Integer temp;
38
+       while (it.hasNext()) {
39
+           //initialize the counter to 0
40
+           count = 0;
41
+           temp = (Integer) it.next();
42
+
43
+           //loop through the array sorted to count the duplicates
44
+
45
+           for (int j = 0; j < n; j++) {
46
+
47
+               if (temp.intValue() == array[j]) {
48
+
49
+                   count++;
50
+
51
+
52
+               }
53
+
54
+           }
55
+
56
+           map.put(temp, count);
57
+       }
58
+
59
+       for(Map.Entry<Integer,Integer>entry: map.entrySet()){
60
+           if(entry.getValue().intValue() < maxNumberOfDuplications ){
61
+               for (int i=0;  i< entry.getValue().intValue(); i++)
62
+                   arraylist.add(entry.getKey() );
63
+
64
+           }
65
+
66
+       }
67
+       Integer[] integers =arraylist.toArray(new  Integer[arraylist.size() ] ) ;     //return new Integer[0];
68
+
69
+       return  integers ;
70
+
71
+
72
+   }
73
+
74
+
75
+
76
+
77
+
78
+    @Override
79
+    public Integer[] removeDuplicatesExactly(int exactNumberOfDuplications) {
80
+        Arrays.sort (array);
81
+        int n = array.length;
82
+        int count = 0;
83
+        int num=0;
84
+        ArrayList<Integer>arraylist=new ArrayList<>() ;
85
+        //Get all unique values
86
+        HashSet set = new HashSet();
87
+        for (int i=0; i < array.length;i++) {
88
+            set.add(new Integer(array[i]));
89
+        }
90
+
91
+        Map<Integer,Integer>map=new HashMap<>();
92
+        Iterator it = set.iterator();
93
+        // Loop through the set for duplicates count and add it to Map
94
+        Integer temp;
95
+        while (it.hasNext()) {
96
+            //initialize the counter to 0
97
+            count = 0;
98
+            temp = (Integer) it.next();
99
+
100
+            //loop through the array sorted to count the duplicates
101
+
102
+            for (int j = 0; j < n; j++) {
103
+
104
+                if (temp.intValue() == array[j]) {
105
+
106
+                    count++;
107
+
108
+
109
+                }
110
+
111
+            }
112
+
113
+            map.put(temp, count);
114
+        }
115
+
116
+        for(Map.Entry<Integer,Integer>entry: map.entrySet()){
117
+            if(entry.getValue().intValue() != exactNumberOfDuplications){
118
+                    for (int i=0;  i< entry.getValue().intValue(); i++)
119
+                            arraylist.add(entry.getKey() );
120
+
121
+            }
122
+
123
+        }
124
+             Integer[] integers =arraylist.toArray(new  Integer[arraylist.size() ] ) ;     //return new Integer[0];
125
+
126
+        return  integers ;
127
+    }
128
+
129
+
130
+}

+ 89
- 0
src/main/java/com/zipcodewilmington/looplabs/StringDuplicateDeleter.java Parādīt failu

@@ -1,8 +1,97 @@
1 1
 package com.zipcodewilmington.looplabs;
2 2
 
3
+import java.util.*;
4
+
3 5
 /**
4 6
  * Created by leon on 1/28/18.
5 7
  * @ATTENTION_TO_STUDENTS You are forbidden from modifying the signature of this class.
6 8
  */
7 9
 public final class StringDuplicateDeleter extends DuplicateDeleter<String> {
10
+    public StringDuplicateDeleter(String[] intArray) {
11
+        super(intArray);
12
+    }
13
+
14
+    @Override
15
+    public String[] removeDuplicates(int maxNumberOfDuplications) {
16
+        Arrays.sort(array);
17
+        int n = array.length;
18
+        int count;
19
+        ArrayList<String>list=new ArrayList<>() ;
20
+        Map<String,Integer > map=new HashMap<>();
21
+        Set<String> set = new HashSet<String>();
22
+        for (int i = 0; i < n; i++) {
23
+            set.add(array[i]);
24
+        }
25
+        Iterator<String> itr = set.iterator();
26
+        String temp;
27
+        while (itr.hasNext()) {
28
+            count = 0;
29
+            temp =  itr.next();
30
+            for (int j = 0; j < n; j++) {
31
+
32
+                if (temp == array[j]) {
33
+
34
+                    count++;
35
+                }
36
+
37
+
38
+            }
39
+            map.put(temp,count);
40
+
41
+        }
42
+        for(Map.Entry<String,Integer>entry :map.entrySet() ){
43
+            if(entry.getValue().intValue()<maxNumberOfDuplications){
44
+               // for (int i=0;  i< entry.getValue().intValue(); i++)
45
+                    list.add(entry.getKey() );
46
+
47
+
48
+            }
49
+        }
50
+        String[] str=list.toArray(new String[list.size() ]);
51
+
52
+
53
+        return str;
54
+    }
55
+
56
+    @Override
57
+    public String[] removeDuplicatesExactly(int exactNumberOfDuplications) {
58
+
59
+        Arrays.sort(array);
60
+        int n = array.length;
61
+        int count;
62
+        ArrayList<String>list=new ArrayList<>() ;
63
+        Map<String,Integer > map=new HashMap<>();
64
+        Set<String> set = new HashSet<String>();
65
+        for (int i = 0; i < n; i++) {
66
+            set.add(array[i]);
67
+        }
68
+        Iterator<String> itr = set.iterator();
69
+        String temp;
70
+        while (itr.hasNext()) {
71
+            count = 0;
72
+            temp =  itr.next();
73
+            for (int j = 0; j < n; j++) {
74
+
75
+                if (temp == array[j]) {
76
+
77
+                    count++;
78
+                }
79
+
80
+
81
+            }
82
+            map.put(temp,count);
83
+
84
+        }
85
+        for(Map.Entry<String,Integer>entry :map.entrySet() ){
86
+            if(entry.getValue().intValue() != exactNumberOfDuplications){
87
+                for (int i=0;  i< entry.getValue().intValue(); i++)
88
+                    list.add(entry.getKey() );
89
+
90
+
91
+            }
92
+        }
93
+            String[] str=list.toArray(new String[list.size() ]);
94
+
95
+        return str;
96
+    }
8 97
 }

+ 1
- 0
src/test/java/com/zipcodewilmington/looplabs/IntegerDuplicateDeleterTest.java Parādīt failu

@@ -48,6 +48,7 @@ public class IntegerDuplicateDeleterTest {
48 48
 
49 49
     @Test
50 50
     public void testRemoveDuplicatesExactly3() {
51
+        //0, 0, 0, 1, 2, 2, 4, 4, 5, 5, 5, 6, 9, 9, 9
51 52
         Integer[] expected = new Integer[]{1, 2, 2, 4, 4, 6};
52 53
         deleter.removeDuplicates(3);
53 54
         deleter.removeDuplicatesExactly(2);