I need help with removing duplicate permutations in java. It is very important t
ID: 3588394 • Letter: I
Question
I need help with removing duplicate permutations in java. It is very important that there is no usage of SETS or any other implementation of SETS!
What I have so far.
package permutation;
import java.io.*; // needed for File, FileReader, and BufferedReader classes
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
/**
*
* @author kevin
*/
public class permutationsconsole {
private static ArrayList createDictionary() throws FileNotFoundException
{
FileReader dictionary = new FileReader("wordsEn.txt");
BufferedReader reader = new BufferedReader(dictionary);
ArrayList <String> words = new ArrayList<>();
String line = null; // to store each of the values and place them in the arraylist
try {
while((line = reader.readLine()) != null) // as long as reader can find the next word, keep iterating.
{
words.add(line);
}
} catch (IOException ex)
{
ex.printStackTrace();
}
return words; // this will return the list of valid english words from the text file
}
private static ArrayList filterEnglishWords(ArrayList <String> l) throws FileNotFoundException
{
ArrayList<String> valid = createDictionary();
l.retainAll(valid);
return l; // this needs to be corrected
}
private ArrayList <String> removeDuplicateWords(ArrayList <String> words)
{
// I need help implementing this method
}
private void writeListToFile(ArrayList<String> data) throws IOException
{
FileWriter writer = new FileWriter("/yourpath/to/output.txt", true);
for (String str : data) {
writer.write(str);
writer.write(" ");
}
writer.close();
}
private void writeStringToFile(String data) throws IOException
{
FileWriter writer = new FileWriter("/yourpath/to/output.txt", true); // this needs to be corrected as well.
writer.write(data);
writer.write(" ");
writer.close();
}
public static void main(String args[]) throws FileNotFoundException
{
File output = new File("perms.dat"); // file that will hold the permutations
ArrayList dictionary = createDictionary(); // the ArrayList that is returned by this method is stored in a new ArrayList.
Scanner kbd = new Scanner(System.in);
boolean isDone = false;
do
{
System.out.println("Type in a word and this program will calculate its permutations.");
String word = kbd.nextLine();
for(int i = 0; i < 1; i++)
{
permutationscalculator pc = new permutationscalculator(word);
}
System.out.println("Would you like to continue?(y/n)");
char quit = kbd.nextLine().charAt(0);
if(quit == 'y')
{
isDone = true;
}
else
{
isDone = false;
}
}while(isDone == true);
}
}
Explanation / Answer
Solution is basically creating new Array list and add word to this array list if it si not already present. Check code in bold.
import java.io.*; // needed for File, FileReader, and BufferedReader classes
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
/**
*
* @author kevin
*/
public class permutationsconsole {
private static ArrayList createDictionary() throws FileNotFoundException
{
FileReader dictionary = new FileReader("wordsEn.txt");
BufferedReader reader = new BufferedReader(dictionary);
ArrayList <String> words = new ArrayList<>();
String line = null; // to store each of the values and place them in the arraylist
try {
while((line = reader.readLine()) != null) // as long as reader can find the next word, keep iterating.
{
words.add(line);
}
} catch (IOException ex)
{
ex.printStackTrace();
}
return words; // this will return the list of valid english words from the text file
}
private static ArrayList filterEnglishWords(ArrayList <String> l) throws FileNotFoundException
{
ArrayList<String> valid = createDictionary();
l.retainAll(valid);
return l; // this needs to be corrected
}
private ArrayList <String> removeDuplicateWords(ArrayList <String> words)
{
ArrayList<String> distinct = new ArrayList<>();
for (String word : words) {
if (!distinct.contains(word)) {
distinct.add(word);
}
}
return distinct;
// I need help implementing this method
}
private void writeListToFile(ArrayList<String> data) throws IOException
{
FileWriter writer = new FileWriter("/yourpath/to/output.txt", true);
for (String str : data) {
writer.write(str);
writer.write(" ");
}
writer.close();
}
private void writeStringToFile(String data) throws IOException
{
FileWriter writer = new FileWriter("/yourpath/to/output.txt", true); // this needs to be corrected as well.
writer.write(data);
writer.write(" ");
writer.close();
}
public static void main(String args[]) throws FileNotFoundException
{
File output = new File("perms.dat"); // file that will hold the permutations
ArrayList dictionary = createDictionary(); // the ArrayList that is returned by this method is stored in a new ArrayList.
Scanner kbd = new Scanner(System.in);
boolean isDone = false;
do
{
System.out.println("Type in a word and this program will calculate its permutations.");
String word = kbd.nextLine();
for(int i = 0; i < 1; i++)
{
permutationscalculator pc = new permutationscalculator(word);
}
System.out.println("Would you like to continue?(y/n)");
char quit = kbd.nextLine().charAt(0);
if(quit == 'y')
{
isDone = true;
}
else
{
isDone = false;
}
}while(isDone == true);
}
}