-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveRepeatElement.java
More file actions
35 lines (24 loc) · 1.26 KB
/
RemoveRepeatElement.java
File metadata and controls
35 lines (24 loc) · 1.26 KB
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
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
public class RemoveRepeatElement {
/*Program to identify and remove all repeated elements from an array.
Arrays could be of various types like integer, character or string.*/
public static void main(String[] args) {
Integer[] intArray = {1, 2, 2, 3, 4, 4, 5};
Integer[] newIntArr = removeDuplicates(intArray );
System.out.println("New int array: "+Arrays.toString(newIntArr));
Character[] charArray = {'a', 'b', 'a', 'c', 'd', 'd'};
Character[] uniqueChars = removeDuplicates(charArray);
System.out.println("Unique characters: " + Arrays.toString(uniqueChars));
String[] strArray = {"apple", "banana", "apple", "orange", "banana"};
String[] uniqueStrings = removeDuplicates(strArray);
System.out.println("Unique strings: " + Arrays.toString(uniqueStrings));
}
static <T> T[] removeDuplicates(T[] array){
HashSet<T> uniqueElements = new LinkedHashSet<>(Arrays.asList(array));
System.out.println("uniqueElements: "+uniqueElements);
T[] result = uniqueElements.toArray(Arrays.copyOf(array, uniqueElements.size()));
return result;
}
}