NiraParikh преди 5 години
родител
ревизия
f188f7e20f

+ 94
- 1
src/main/java/com/zipcodewilmington/looplabs/IntegerDuplicateDeleter.java Целия файл

@@ -1,8 +1,101 @@
1 1
 package com.zipcodewilmington.looplabs;
2 2
 
3
+import java.util.Arrays;
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.
6 8
  */
7 9
 public final class IntegerDuplicateDeleter extends DuplicateDeleter<Integer> {
8
-}
10
+
11
+    public IntegerDuplicateDeleter(Integer[] intArray) {
12
+        super(intArray);
13
+    }
14
+
15
+    @Override
16
+    public Integer[] removeDuplicates(int maxNumberOfDuplications) {
17
+
18
+        Integer [] result = new Integer[array.length];
19
+        int count = 0;
20
+        for (int i = 0; i < array.length ; i++) {
21
+            if(itemCount(array[i]) < maxNumberOfDuplications){
22
+                result[count] = array[i];
23
+                count++;}
24
+            }
25
+            return Arrays.copyOfRange(result,0,count);
26
+        }
27
+
28
+//        Integer[] arr = Arrays.copyOf(array,array.length);
29
+//        StringBuilder stringBuilder = new StringBuilder();
30
+//
31
+//        for (int i = 0; i < arr.length ; i++) {
32
+//            int count = 1;
33
+//            for (int j = 0; j < arr.length; j++) {
34
+//                if (arr[j].equals(arr[i])) {
35
+//                    count++;
36
+//                }
37
+//            }
38
+//            if (count <= maxNumberOfDuplications)
39
+//                stringBuilder.append(arr[i]).append("/");
40
+//        }
41
+//        if(!stringBuilder.toString().equals("")){
42
+//            arr =getNewArr(stringBuilder);
43
+//        }else{
44
+//            arr = new Integer[0];
45
+//        }
46
+//        return arr;
47
+//    }
48
+
49
+
50
+    private int itemCount(int item) {
51
+        int counter = 0;
52
+        for (int items : array) {
53
+            if (items == item)
54
+                counter++;
55
+        }
56
+        return counter;
57
+    }
58
+//    private Integer[] getNewArr(StringBuilder stringBuilder) {
59
+//        Integer [] arr;
60
+//        String [] strings = stringBuilder.toString().split("/");
61
+//        arr = new Integer[strings.length];
62
+//        for (int i = 0; i < arr.length; i++)
63
+//            arr[i] = Integer.valueOf(strings[i]);
64
+//            return arr;
65
+//    }
66
+
67
+    @Override
68
+    public Integer[] removeDuplicatesExactly(int exactNumberOfDuplications) {
69
+
70
+        Integer [] result = new Integer[array.length];
71
+        int count = 0;
72
+        for (int i = 0; i < array.length ; i++) {
73
+            if(itemCount(array[i]) != exactNumberOfDuplications){
74
+                result[count] = array[i];
75
+                count++;
76
+            }
77
+        }
78
+        return Arrays.copyOfRange(result,0,count);
79
+    }
80
+
81
+
82
+//        StringBuilder stringBuilder = new StringBuilder();
83
+//        Integer [] arr = Arrays.copyOf(array,array.length);
84
+//        for (int i = 0; i < arr.length; i++) {
85
+//            int count = 0;
86
+//            for (int j = 0; j < arr.length; j++) {
87
+//                if (arr[j].equals(arr[i])) {
88
+//                    count++;
89
+//                }
90
+//            }
91
+//            if (count != exactNumberOfDuplications)
92
+//                stringBuilder.append(arr[i]).append("/");
93
+//        }
94
+//        if(!stringBuilder.toString().equals("")){
95
+//            arr =getNewArr(stringBuilder);
96
+//        }else{
97
+//            arr = new Integer[0];
98
+//        }
99
+//        return arr;
100
+    }
101
+

+ 55
- 1
src/main/java/com/zipcodewilmington/looplabs/StringDuplicateDeleter.java Целия файл

@@ -1,8 +1,62 @@
1 1
 package com.zipcodewilmington.looplabs;
2 2
 
3
+import java.util.Arrays;
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> {
8
-}
10
+    public StringDuplicateDeleter(String[] intArray) {
11
+
12
+        super(intArray);
13
+    }
14
+
15
+    @Override
16
+    public String[] removeDuplicates(int maxNumberOfDuplications) {
17
+
18
+        String[] resultArray= new String[array.length];
19
+        Integer count=0;
20
+        for(Integer i=0;i<array.length;i++){
21
+            if(itemcount(array[i]) < maxNumberOfDuplications){
22
+                resultArray[count]=array[i];
23
+                count++;
24
+            }
25
+        }
26
+        return Arrays.copyOfRange(resultArray,0,count);
27
+    }
28
+    @Override
29
+    public String[] removeDuplicatesExactly(int exactNumberOfDuplications) {
30
+        String[] resultArray= new String[array.length];
31
+        int count=0;
32
+        for(Integer i=0;i<array.length;i++){
33
+            if(itemcount(array[i])!= exactNumberOfDuplications){
34
+                resultArray[count]=array[i];
35
+                count++;
36
+
37
+            }
38
+        }
39
+        return Arrays.copyOfRange(resultArray,0,count);
40
+    }
41
+
42
+
43
+    private String[] getNewArr(StringBuilder stringBuilder) {
44
+        String [] arr;
45
+        String[] strings = stringBuilder.toString().split("/");
46
+        arr = new String[strings.length];
47
+        for (int i = 0; i < arr.length; i++)
48
+            arr[i] =strings[i];
49
+            return arr;
50
+         }
51
+    public int itemcount(String value){
52
+        int count=0;
53
+        for(String element:array){
54
+            if(element.equals(value)){
55
+                count++;
56
+            }
57
+        }
58
+        return count;
59
+    }
60
+    }
61
+
62
+