Write a program to remove duplicate numbers from given array.
public class RemoveDuplicateNumbers {
public static void main(String a[]){
int[] input1 = {2,3,6,6,8,9,10,10,10,12,12};
int[] output = removeDuplicates(input1);
for(int i:output){
System.out.print(i+" ");
}
}
private static int[] removeDuplicates(int[] input) {
int j = 0;
int i = 1;
//return if the array length is less than 2
if(input.length < 2){
return input;
}
while(i < input.length){
if(input[i] == input[j]){
i++;
}else{
input[++j] = input[i++];
}
}
int[] op = new int[j+1];
for(int k=0; k<op.length; k++){
op[k] = input[k];
}
return op;
}
}
Write a program to remove duplicate characters from array
public class RemoveDuplicateChars {
public static void main(String[] args) {
char c[]={'a','b','g','c','d','e','a','f','c','g'};
RemoveDuplicateChars obj = new RemoveDuplicateChars();
obj.method1(c);
}
public void method1(char a[]) {
String s=new String(a);
String temp="";
for(int i=0;i<s.length();i++) {
String s1=s.substring(i, i+1);
if(temp.contains(s1)) {
continue;
} else {
temp+=s1;
}
}
System.out.println(temp);
}
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.