Skip to content

Instantly share code, notes, and snippets.

@pepet96
Created March 13, 2014 01:35
Show Gist options
  • Save pepet96/9520361 to your computer and use it in GitHub Desktop.
Save pepet96/9520361 to your computer and use it in GitHub Desktop.
import java.util.Scanner;
import java.util.*;
public class dedup{
public static void main(String[ ] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of words that will be analyzed");
int arrlength = input.nextInt();
String[] array = new String[arrlength];
System.out.println("Enter the words now");
for (int i = 0; i < array.length; i++) {
array[i] = input.nextLine();
}
String[] sortedarray = sortStringBubble(array);
String[] deduparray = deletedups(sortedarray);
for ( int k = 0; k < sortedarray.length; k++ ){
System.out.println( sortedarray[ k ] );
}
}
public static String[] sortStringBubble( String array[])
{
int j;
boolean flag = true;
String temp;
while ( flag )
{
flag = false;
for ( j = 0; j < array.length - 1; j++ )
{
if ( array[ j ].compareTo( array[ j+1 ] ) > 0 )
{
temp = array [ j ];
array [ j ] = array [ j+1];
array [ j+1] = temp;
flag = true;
}
}
}
return array;
}
public static String[] deletedups(String sortedarray[]){
for(int i = 0;i < sortedarray.length-1; i++){
if (sortedarray[i].equals(sortedarray[i+1]))
{
sortedarray[i+1] = " ";
}
else
{
continue;
}
}
return sortedarray;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment