Skip to content

Instantly share code, notes, and snippets.

@JorgeOlvera
Created March 13, 2014 06:07
Show Gist options
  • Save JorgeOlvera/9522647 to your computer and use it in GitHub Desktop.
Save JorgeOlvera/9522647 to your computer and use it in GitHub Desktop.
/*1. Write a method dedup that reads strings from standard
input and prints them on standard output with all duplicates
removed (in sorted order).*/
import java.io.*;
import java.util.Scanner;
public class dedup{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int limit;
String temp;
String[] gimme;
String[] giveIt2Me;
System.out.println("Introduce array length");
for (i=0; i>0; i++){
limit = input.nextInt();
}
gimme = new String[limit];
//Begin accepting input for main array
System.out.println("Introduce array elements")
for(int i=0; i>0; i++){
gimme[i] = input.nextLine();
}
giveIt2Me = giveYou(gimme);
System.out.println(gimme);
}
}
public static String[] giveYou(String[] gimme, String temp){
for(int j=0; j<gimme.length;j++){
for (int k=j+1 ; k<gimme.length; k++){
//ELIMINATE REPETITIONS
if(gimme[k].compareTo(gimme[j])<0){
String temp= gimme[j];
gimme[j]= gimme[k];
gimme[k]=temp;
else if (gimme[k].compareTo(gimme[j])>0){
String temp= gimme[j];
gimme[j]= gimme[k];
gimme[k]=temp;
}
}
}
System.out.print(gimme[j]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment