GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Java_Algorithm_Coding_Practise_Example/RmoveDulicte.java at master · mehagarg/Java_Algorithm_Coding_Practise_Example

Expand file tree

/

RmoveDulicte.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
79 lines (69 loc) · 2.04 KB
/

RmoveDulicte.java

Copy path

File metadata and controls

79 lines (69 loc) · 2.04 KB
Raw
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import java.util.Arrays;
import java.util.HashSet;
public class RmoveDulicte {
public static void main(String[] args){
String a = "aaaaabbbb";
// System.out.println(removeDupsNoSort(a));
System.out.println(removeUsingSet(a));
}
/**
* Removes duplicate characters from input string.
* This function first sorts the input string then removes dups by comparing adjacent characters
* In place sorting. No additional buffer.
* Sort is O(n logn). Then the function loops through each character. So O(n logn) + O(n) time
* @param str
* @return
*/
public static String removeUsingSet(String s){
char[] c = s.toCharArray();
HashSet<Character> val = new HashSet<Character>();
for(char c1:c){
val.add(c1);
}
return new String(val.toString());
}
public static String removeDups(String str){
if(str == null || str.length() == 1){
return str;
}
char[] input = str.toCharArray();
Arrays.sort(input);
int current = 0;
int next = 1;
while(true){
while(next<=input.length-1 && input[current] == input[next]){
next++;
}
if(next >= input.length){
break;
}
input[++current] = input[next++];
}
return new String(input,0,current+1);
}
/**
* Removes dups without sorting the input string
* O(n-square) time
* @param str
* @return
*/
public static String removeDupsNoSort(String str){
if(str == null || str.length() <= 1){
return str;
}
char[] input = str.toCharArray();
int i=0;
for(int j=1;j<input.length;j++){
if(!isPresent(i,j,input)){
input[++i] = input[j];
}
}
return new String(input,0,++i);
}
private static boolean isPresent(int end, int j, char[] a){
for(int i=0;i<=end;i++){
if(a[i] == a[j]) return true;
}
return false;
}
}
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.