-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemoving duplicate values using Generic methods
65 lines (57 loc) · 1.48 KB
/
Removing duplicate values using Generic methods
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import java.util.ArrayList;
import java.util.List;
/*
by mohammed shafiq fata
edited on 22/04/2021
*/
public class CommonElementsInTwoElementOfStringArray {
public static void main(String[] args) {
String[] array = { "1,2,3,4,5,8,9", "0,5,6,7" };
String firstCellArrayElement = filterFirstElementOfArray(array);
String secondCellArrayElement = filterSecondElementOfArray(array);
List<String> list = compareTwoString(firstCellArrayElement, secondCellArrayElement);
list.forEach(v -> System.out.print(v + " "));
}
/*
* compare two string against each other
*/
public static List<String> compareTwoString(String s1, String s2) {
List<String> list = new ArrayList<String>();
for (int i = 0; i < s1.length(); i++) {
for (int j = 0; j < s2.length(); j++) {
if (s1.charAt(i) == s2.charAt(j)) {
list.add("" + s1.charAt(i));
}
}
}
return list;
}
/*
* filtering commas from string first element
*/
public static String filterFirstElementOfArray(String... array) {
String a = array[0];
char[] c = a.toCharArray();
String result = "";
for (int i = 0; i < c.length; i++) {
if (c[i] != ',') {
result += c[i];
}
}
return result;
}
/*
* filtering commas from string second element
*/
public static String filterSecondElementOfArray(String... array) {
String a = array[1];
char[] c = a.toCharArray();
String result = "";
for (int i = 0; i < c.length; i++) {
if (c[i] != ',') {
result += c[i];
}
}
return result;
}
}