Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
42 lines (35 loc) · 998 Bytes

File metadata and controls

42 lines (35 loc) · 998 Bytes
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package Arrays;
import java.util.HashSet;
import java.util.Set;
public class removeDuplicates {
/**
* Given an array arr, remove the duplicates from arr
* @param arr
*/
public static void removeDuplicates(int [] arr){
Set<Integer> st = new HashSet<Integer>();
for(int i=0;i<arr.length;i++){
if(!st.contains(arr[i])){
st.add(arr[i]);
}else {
arr[i] = 0;
}
}
// convert back to array
int [ ] result = new int[st.size()];
int i = 0;
for (int num:
st) {
result[i++] = num;
}
// Print the new array
System.out.print("New Array without Duplicates: ");
for (int j = 0; j < result.length; j++) {
System.out.print(result[j] + " ");
}
}
public static void main(String [] args){
int [] arr = {2,4,5,7,6,4,6,12,11,13,12,7};
removeDuplicates(arr);
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.