Label distinct values for Arraylist<String> with identifier - java

I have a simple problem. I have an ArrayList (of type String) with 100'000 Names. I want to create another ArrayList (of type Integer) which also has 100'000 Elements and assignes each Element of the String ArrayList an ID-Number. Equal Names should have equal ID Numbers assigned. VERY BASIC EXAMPLE:
i have: (hans, max, hans, hans, frank)
i want: ( 1 , 2 , 1 , 1 , 3 )
I implemented a Solution which works, but is very slow (for my big dataset of 100'000 Names). I wonder someone can find a better/faster way of doing this. Thanks everyone for any tipps!
public static void main(String[] args) {
// initialize arraylists
ArrayList<String> Names = new ArrayList<String>();
ArrayList<Integer> Id = new ArrayList<Integer>();
// sample data // I want the integer Arraylist to have values:
Names.add("Hans"); // 1
Names.add("Max"); // 2
Names.add("Hans"); // 1
Names.add("Hans"); // 1
Names.add("Frank"); // 3
// my solution (works, but is slow and confusing)
int N = Names.size();
int ID_Count = 0;
for (int i=0; i<N; i++) {
boolean match_found = false;
String curr_Name = Names.get(i);
for (int check=0; check<i; check++) {
if (curr_Name.equals(Names.get(check))) {
Id.add(Id.get(check));
match_found = true;
break;
}
}
if (match_found==false) {
ID_Count++;
Id.add(ID_Count);
}
}
// show result
for (int i=0; i<N; i++) {
System.out.println(Id.get(i) + " " + Names.get(i));
}
}

This is a faster way of doing it:
public static void main(String[] args) {
// initialize arraylists
ArrayList<String> Names = new ArrayList<String>();
Map<String,Integer> map = new HashMap<>();
// sample data // I want the integer Arraylist to have values:
Names.add("Hans"); // 1
Names.add("Max"); // 2
Names.add("Hans"); // 1
Names.add("Hans"); // 1
Names.add("Frank"); // 3
int N = Names.size();
int id = 0;
for (int i = 0; i < N; i++) {
String name = Names.get(i);
if (map.get(name) == null) {
map.put(name,++id);
}
}
// show result
for (int i=0; i<N; i++) {
String name = Names.get(i);
System.out.println(map.get(name) + " " +name);
}
}

If I understand your question correctly, you ought to use a HashMap, as it will be a lot faster/easier than working with two arrays. I would do it like this:
Map<String,Integer> map = new HashMap<>();
This way you will be able to do map.put("name", int id), and it won't allow duplicate keys (key is the "name" in your case) so you will automatically have only one key/value pair for names that are the same.

Related

JAVA 2D Array Loop Print Formatting [0][0], [1][1], etc

I am having trouble getting my 2D array to print side by side values taking the first array row value index 0 and matching it to the 2nd row array index 0 and continuing on in a vertical list. As it stands, my code compiles and just prints the entire arrays in a horizontal fashion on two lines. Basically, I would like the end result to be formatted like this:
Admin | Password1
Vale.Vicky | BruceIsTheBat!
Lane.Lois | FlyMeToTheMoon1234
Kent.Clark | PhoneBoothsSmell
Wayne.Bruce | ThisBat4You99
...and so on.
public class PasswordArrays { // start of class
public static void main(String[] args) { // Start of main
// TODO Auto-generated method stub
String [][] idArray = {
{"Admin", "Vale.Vicky", "Lane.Lois", "Kent.Clark", "Wayne.Bruce", "Parker.Peter", "Rogers.Steve", "Luther.Lex", "Osborn.Harry","Prince.Diana", "Linda Zoel"},
{"Password1", "BruceIsTheBat!", "FlyMeToTheMoon1234", "PhoneBoothsSmell","ThisBat4You99", "webSlinger","ShieldEnthusiast", "HairClub4Men", "GoblinGoober", "WonderWonderWho?", "WhoIsLindaZoel?"}
};
printArray(idArray);
} //End of main
public static void printArray(String a [][]) { //start of printArray method
for (int row=0; row < a.length ; row++) { // start of row for loop
for (int column = 0; column < a [row].length; column++) { //start of column for loop
System.out.print(a[row][column] + " ");
} // End of column for loop
System.out.println();
} // End of row for loop
} // End of printArray method
} //End of class
I know there's got to be an answer already for this somewhere but I have been unsuccessful in finding it. Any help is greatly appreciated.
A 2D array could be iterated in two ways:
Row wise
Column wise
As per your expected result, what you would want to do is iterate column wise rather than row wise. Here's a solution:
public static void printArray(String a [][]) {
for(int col=0; col < a[0].length; col++) {
for (int row = 0; row < a.length; row++) {
if (row!=a.length-1) {
System.out.print(a[row][col] + "|");
}
else {
System.out.print(a[row][col]);
}
}
System.out.println();
}
}
Helpful Link: Iterate through multi-dimensional array
you are traversing your 2D array incorrectly
you can try this for your array:
if(a.length > 0){
for(int i = 0; i < a[0].length; i++){
for(int j = 0;j < a.length; j++){
System.out.print(a[j][i]+" ");
}
System.out.println("");
}
}
Structure
Are you sure that you understood the structure of your 2d-array? You only have two rows but multiple columns (the array contains 2 arrays containing multiple elements each).
The structure is
idArray = { /* firstArray, secondArray */ }
idArray[0] = { /* Names */ }
idArray[1] = { /* Passwords */ }
Solution
You only need one iteration from int i = 0; to i < idArray[0].length. After that always pair the content of idArray[0][i] with idArray[1][i], that's all.
final int[][] idArray = ...
// Variant 1: Regular for-loop
for (int i = 0; i < idArray[0].length; i++) {
System.out.println(idArray[0][i] + " | " + idArray[1][i];
}
// Variant 2: Stream solution
IntStream.range(0, idArray[0].length) // IntStream
.map(i -> idArray[0][i] + " | " + idArray[1][i]) // Stream<String>
.forEach(System.out::println);
Note that idArray[0].length and idArray[1].length should of course be equal.
Notes
Probably you are better off using proper OOP, creating classes for each Account containing name and password fields and then using a List<Account> and toString for Account or something like that.
public class Account {
private final String mName;
private final String mPassword;
public Account(final String name, final String password) {
this.mName = name;
this.mPassword = password;
}
public String getName() {
return this.mName;
}
public String getPassword() {
return this.mPassword;
}
#Override
public String toString() {
return this.getName() + " | " + this.getPassword();
}
}
And then use it like
final List<Account> accounts = new ArrayList<>();
accounts.add(new Account("Admin", "Password1"));
...
// Variant 1: Enhanced for-loop 'foreach'
for (final Account account : accounts) {
System.out.println(account);
}
// Variant 2: The same using streams
accounts.forEach(System.out::println);
// Variant 3: Using Lists 'toString'
System.out.println(accounts);

Compare arraylists for non identical elements

Hello there I am being confused in comparing two array lists, one of my array list is as:
private ArrayList<String> members = new ArrayList<>();
members.add("member123keyxyzmember123 number");
members.add("member456keyxyzmember456 number");
members.add("member789keyxyzmember789 number");
members.add("member2233keyxyzmember2233 number");
members.add("member1122keyxyzmember1122 number");
The second arraylist is as:
private ArrayList<String> syncMembers = new ArrayList<>();
syncMembers.add("member123keyxyz123statuskeyxyz123photokeyxyzmember123 number");
syncMembers.add("member456keyxyz456statuskeyxyz456photokeyxyzmember456 number");
The problem is that I am comparing both so that they give me the numbers that are in members list and are not in syncMembers list!
That is the out put should be:
member789 number
member2233 number
member1122 number
only!
What I have been trying is:
for (int i = 0; i < members.size(); i++) {
String stringFromMembersList = members.get(i);
String[] memberParts = stringFromMembersList.split("keyxyz");
String memberNumber = memberParts[1];
//Log.e("hgax", "sync:::" + memberNumber);
for (int j = 0; j < syncMembers.size(); j++) {
String stringFromSyncList = syncMembers.get(j);
String[] syncParts = stringFromSyncList.split("keyxyz");
String n = syncParts[3];
if (memberNumber.equals(n)) {
//Log.e("hgax", "hee:::" + n);
break;
} else {
Log.e("hgax", "ssshee:::" + memberNumber);
}
}
}
The output I am getting is:
member456 number
member789 number
member789 number
member2233 number
member2233 number
member2233 number
member1122 number
member1122 number
member1122 number
member1122 number
I am bit confuse what is happeing to me and What i have been doing wrong? Can somebody please tell what blunder I am doing Thanks in advance
Try to think about what you need to check to reach your goal. In order to determine that a member in the list members does not exist in syncMembers you have to check the entirety of the syncMembers list for that member. Since the lists are not identical (as your question states), you cannot use Collection.contains(Object o).
This should achieve your goal:
// We need this initial check as if syncMembers is empty it'll display all members
// And why bother to do this if syncMembers is empty anyway!
if (!syncMembers.isEmpty()) {
for (String member : members) {
String memberNo = member.split("keyxyz")[1];
int i = 0;
boolean found = false;
while (!found && i < syncMembers.size()) {
// Iterate over syncMembers until a match if found
// or we have exhausted the list
found = syncMembers.get(i).split("keyxyz")[3].equals(memberNo);
i++;
}
if (!found) {
// Display only if not found
System.out.println(memberNo);
}
}
}
Edit: updated answer to include the original mixed values in the arrayList
ArrayList<String> membersList = new ArrayList<>();
ArrayList<String> syncMembersList = new ArrayList<>();
for (int i = 0; i < members.size(); i++) {
String s = members.get(i).substring(members.get(i).lastIndexOf("member"));
membersList.add(s);
}
for (int j = 0; j < syncMembers.size(); j++) {
String s = syncMembers.get(j).substring(syncMembers.get(j).lastIndexOf("member"));
syncMembersList.add(s);
}
for (int i = 0; i < membersList.size(); i++) {
if (!syncMembersList.contains(membersList.get(i))) {
System.out.println(membersList.get(i));
}
}
This will print the output you require.

Remove duplicates in 2d array

I want to remove duplicate row in a 2d array . i tried the below code .but it is not working . please help me .
Input :
1,ram,mech
1,ram,mech
2,gopi,csc
2.gopi,civil
output should be :
1,ram,mech
2,gopi,csc
2.gopi,civil
Code :
package employee_dup;
import java.util.*;
public class Employee_dup {
public static void main(String[] args)
{
boolean Switch = true;
System.out.println("Name ID Dept ");
String[][] employee_t = {{"1","ram","Mech"},{"1","siva","Mech"},{"1","gopi","Mech"},{"4","jenkat","Mech"},{"5","linda","Mech"},{"1","velu","Mech"}};
int g = employee_t[0].length;
String[][] array2 = new String[10][g];
int rows = employee_t.length;
Arrays.sort(employee_t, new sort(0));
for(int i=0;i<employee_t.length;i++){
for(int j=0;j<employee_t[0].length;j++){
System.out.print(employee_t[i][j]+" ");
}
System.out.println();
}
List<String[]> l = new ArrayList<String[]>(Arrays.asList(employee_t));
for(int k = 0 ;k < employee_t.length-1;k++)
{
if(employee_t[k][0] == employee_t[k+1][0])
{
System.out.println("same value is present");
l.remove(1);
array2 = l.toArray(new String[][]{});
}
}
System.out.println("Name ID Dept ");
for(int i=0;i<array2.length;i++){
for(int j=0;j<array2[0].length;j++){
System.out.print(array2[i][j]+" ");
}
System.out.println();
}
}
}
class sort implements Comparator {
int j;
sort(int columnToSort) {
this.j = columnToSort;
}
//overriding compare method
public int compare(Object o1, Object o2) {
String[] row1 = (String[]) o1;
String[] row2 = (String[]) o2;
//compare the columns to sort
return row1[j].compareTo(row2[j]);
}
}
First I sorted the array based on column one ,then tried to remove duplicates by checking the first column elements and seconds column elements but it is not removing the required column but remove other columns.
You may give this solution a try:
public static void main(String[] args) {
String[][] employee_t = {
{"1","ram","Mech"},
{"1","ram","Mech"},
{"1","siva","Mech"},
{"1","siva","Mech"},
{"1","gopi","Mech"},
{"1","gopi","Mech"} };
System.out.println("ID Name Dept");
Arrays.stream(employee_t)
.map(Arrays::asList)
.distinct()
.forEach(row -> System.out.printf("%-3s%-7s%s\n", row.get(0), row.get(1), row.get(2)));
}
Output
ID Name Dept
1 ram Mech
1 siva Mech
1 gopi Mech
How it works: comparing arrays does rely on instance equality and not on comparing contained elements by equals. Hence converting each row of your 2D array into a List will enable you to compare lists, which takes equals of the elements contained into account.
The Java Stream API does provide a method distinct which relies on equals and will remove all duplicates for you.
Based on your code. Maybe it is not the BEST solution but it works.
public static void main(String[] args) {
System.out.println("Name ID Dept ");
// I added duplicated rows
String[][] inputArray = {
{ "1", "ram", "Mech" },
{ "1", "siva", "Mech" },
{ "1", "gopi", "Mech" },
{ "1", "gopi", "Mech" },
{ "4", "jenkat", "Mech" },
{ "5", "linda", "Mech" },
{ "1", "velu", "Mech" },
{ "1", "velu", "Mech" }
};
// I will add all rows in a Set as it doesn't store duplicate values
Set<String> solutionSet = new LinkedHashSet<String>();
// I get all rows, create a string and insert into Set
for (int i = 0 ; i < inputArray.length ; i++) {
String input = inputArray[i][0]+","+inputArray[i][1]+","+inputArray[i][2];
solutionSet.add(input);
}
// You know the final size of the output array
String[][] outputArray = new String[solutionSet.size()][3];
// I get the results without duplicated values and reconvert it to your format
int position = 0;
for(String solution : solutionSet) {
String[] solutionArray = solution.split(",");
outputArray[position][0] = solutionArray[0];
outputArray[position][1] = solutionArray[1];
outputArray[position][2] = solutionArray[2];
position++;
}
System.out.println("Name ID Dept ");
for (int i = 0; i < outputArray.length; i++) {
for (int j = 0; j < outputArray[0].length; j++) {
System.out.print(outputArray[i][j] + " ");
}
System.out.println();
}
}
I have posted what I think is a readable and easy to maintain solution.
I decided to use distinct from Stream which is part of Java 8
Returns a stream consisting of the distinct elements (according to Object.equals(Object)) of this stream. - https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#distinct--
Main.class
class Main {
public static void main(String[] args)
{
//Create a list of Employee objects
List<Employee> employeeList = new ArrayList<Employee>();
Employee e1 = new Employee(1, "ram", "mech");
Employee e2 = new Employee(1, "ram", "mech");
Employee e3 = new Employee(2, "gopi", "csc");
Employee e4 = new Employee(2, "gopi", "civil");
employeeList.add(e1);
employeeList.add(e2);
employeeList.add(e3);
employeeList.add(e4);
System.out.println("Before removing duplicates");
employeeList.stream().forEach(System.out::println);
//This is where all the magic happens.
employeeList = employeeList.stream().distinct().collect(Collectors.toList());
System.out.println("\nAfter removing duplicates");
employeeList.stream().forEach(System.out::println);
}
}
Output:
Before removing duplicates
Employee [valA=1, valB=ram, valC=mech]
Employee [valA=1, valB=ram, valC=mech]
Employee [valA=2, valB=gopi, valC=csc]
Employee [valA=2, valB=gopi, valC=civil]
After removing duplicates
Employee [valA=1, valB=ram, valC=mech]
Employee [valA=2, valB=gopi, valC=csc]
Employee [valA=2, valB=gopi, valC=civil]
Employee.class
//This is just a regular POJO class.
class Employee {
int valA;
String valB, valC;
public Employee(int valA, String valB, String valC){
this.valA = valA;
this.valB = valB;
this.valC = valC;
}
public Employee(Employee e) {
this.valA = e.valA;
this.valB = e.valB;
this.valC = e.valC;
}
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + valA;
result = prime * result + ((valB == null) ? 0 : valB.hashCode());
result = prime * result + ((valC == null) ? 0 : valC.hashCode());
return result;
}
#Override
public boolean equals(Object obj) {
if(obj instanceof Employee && ((Employee)obj).hashCode() == this.hashCode()){
return true;
}
return false;
}
#Override
public String toString() {
return "Employee [valA=" + valA + ", valB=" + valB + ", valC=" + valC + "]";
}
}
Pre Java - 8 solution. May not be the best way. But a quick solution which works..
String[][] records = {
{"1","ram","Mech"},
{"1","ram","Mech"},
{"1","gopi","csc"},
{"1","gopi","civil"} };
List<String[]> distinctRecordsList = new ArrayList<String[]>();
for(String[] record : records){
if(distinctRecordsList.size()>0){
boolean sameValue = false;
for(String[] distinctRecord : distinctRecordsList){
int distinctRecordFields = distinctRecord.length;
if(record.length==distinctRecordFields){
for(int k=0;k<distinctRecordFields;k++){
sameValue = record[k].equalsIgnoreCase(distinctRecord[k]);
if(!sameValue)
break;
}
}else
throw new Exception("Can't compare the records");
}
if(!sameValue)
distinctRecordsList.add(record);
}else if(distinctRecordsList.size()==0)
distinctRecordsList.add(record);
}
Object[] distRecObjects = distinctRecordsList.toArray();
String[][] distinctRecordsArray = new String[distRecObjects.length][];
int i=0;
for(Object distRecObject : distRecObjects){
distinctRecordsArray[i] = (String[]) distRecObject;
i++;
}
Contrary to some other answers I will try to explain what went wrong in your own code and how to fix it within your code (I agree very much with kkflf that an Employee class would be a huge benefit: it’s more object-oriented and it will help structure the code and give better overview of it).
The issues I see in your code are:
You are not removing the correct element when you detect a duplicate, but always the element at index 1 (the second element since indices count from 0). This isn’t trivial, though, because indices shift as you remove elements. The trick is to iterate backward so only indices that you are finished with shift when you remove an element.
You are using == to compare the first element of the subarrays you are comparing. If you wanted to compare just the first element, you should use equals() for comparison. However, I believe you want to compare the entire row so 2,gopi,csc and 2.gopi,civil are recognized as different and both preserved. Arrays.equals() can do the job.
You need to create array2 only after the loop. As your code stands, if no duplicates are detected, arrays2 is never created.
So your loop becomes:
for (int k = employee_t.length - 1; k >= 1; k--)
{
if (Arrays.equals(employee_t[k], employee_t[k - 1]))
{
System.out.println("same value is present");
l.remove(k);
}
}
array2 = l.toArray(new String[][]{});
This gives you the output you asked for.
Further tips:
Your comparator only compares one field in the inner arrays, which is not enough to guarantee that identical rows come right after each other in the sorted array. You should compare all elements, and also require that the inner arrays have the same length.
Use generics: class Sort extends Comparator<String[]>, and you won’t need the casts in compare()
According to Java naming conventions it should be class EmployeeDup, boolean doSwitch (since switch is a reserved word) and class Sort.
You are not using the variables Switch and rows; delete them.
I have wrote a solution for me. This may not be the best but it works.
public static String[][] removeDuplicate(String[][] matrix) {
String[][] newMatrix = new String[matrix.length][matrix[0].length];
int newMatrixRow = 1;
for (int i = 0; i < matrix[0].length; i++)
newMatrix[0][i] = matrix[0][i];
for (int j = 1; j < matrix.length; j++) {
List<Boolean> list = new ArrayList<>();
for (int i = 0; newMatrix[i][0] != null; i++) {
boolean same = true;
for (int col = 2; col < matrix[j].length; col++) {
if (!newMatrix[i][col].equals(matrix[j][col])) {
same = false;
break;
}
}
list.add(same);
}
if (!list.contains(true)) {
for (int i = 0; i < matrix[j].length; i++) {
newMatrix[newMatrixRow][i] = matrix[j][i];
}
newMatrixRow++;
}
}
int i;
for(i = 0; newMatrix[i][0] != null; i++);
String finalMatrix[][] = new String[i][newMatrix[0].length];
for (i = 0; i < finalMatrix.length; i++) {
for (int j = 0; j < finalMatrix[i].length; j++)
finalMatrix[i][j] = newMatrix[i][j];
}
return finalMatrix;
}
This method will return a matrix without any duplicate rows.

i want to count occurrence of each word in a given sentence in java

beginner at java was asked in an interview
here i have to count the occurrence of each word in a given sentence.
for eg( "chair is equal to chair but not equal to table."
Output : chair :2,
is :1,
equal :2,
to :2,
but :1,
not :1,
table :1 )
I have written some part of the code and tried using for loop but i failed....
public static void main(String[] args)
{
int counter = 0;
String a = " To associate myself with an organization that provides a challenging job and an opportunity to provide innovative and diligent work.";
String[] b =a.split(" "); //stored in array and splitted
for(int i=0;i<b.length;i++)
{
counter=0;
for(int j<b.length;j>0;j--)
{
if(b[i] = b[j])
//
}
}
}
}
Use a hashmap to count frequency of objects
import java.util.HashMap;
import java.util.Map.Entry;
public class Funly {
public static void main(String[] args) {
int counter = 0;
String a = " To associate myself with an organization that provides a challenging job and an opportunity to provide innovative and diligent work.";
String[] b = a.split(" "); // stored in array and splitted
HashMap<String, Integer> freqMap = new HashMap<String, Integer>();
for (int i = 0; i < b.length; i++) {
String key = b[i];
int freq = freqMap.getOrDefault(key, 0);
freqMap.put(key, ++freq);
}
for (Entry<String, Integer> result : freqMap.entrySet()) {
System.out.println(result.getKey() + " " + result.getValue());
}
}
}
Quite easy since Java8:
public static Map<String, Long> countOccurrences(String sentence) {
return Arrays.stream(sentence.split(" "))
.collect(Collectors.groupingBy(
Function.identity(), Collectors.counting()
)
);
}
I would also remove non literal symbols, and convert to lowecase before running:
String tmp = sentence.replaceAll("[^A-Za-z\\s]", "");
So your final main method for interview will be:
ppublic static void main(String[] args) {
String sentence = "To associate myself with an organization that provides a challenging job and an opportunity to provide innovative and diligent work.";
String tmp = sentence.replaceAll("[^A-Za-z\\s]", "").toLowerCase();
System.out.println(
countOccurrences(tmp)
);
}
Output is:
{diligent=1, a=1, work=1, myself=1, opportunity=1, challenging=1, an=2, associate=1, innovative=1, that=1, with=1, provide=1, and=2, provides=1, organization=1, to=2, job=1}
A simple (but not very efficient) way would be to add all the elements to a set, which doesn't allow duplicates. See How to efficiently remove duplicates from an array without using Set. Then iterate through the set and count the number of occurrences in your array, printing out the answer after each set element you check.
There are several solutions to this and I'm not going to provide you with any of them. However, I'm going to give you a rough outline of one possible solution:
You could use a Map, for example a HashMap, where you use the words as keys and the number of their occurrence as values. Then, all you need to do is to split the input string on spaces and iterate over the resulting array. For each word, you check if it already exists in the map. If so you increase the value by one, otherwise you add the word to the map and set the value to 1. After that, you can iterate over the map to create the desired output.
You need to use Map data structure which stores data in key-value pairs.
You can use the HashMap (implementation of Map) to store each word as key and their occurance as the value inside the Map as shown in the below code with inline comments:
String[] b =a.split(" "); //split the array
Map<String, Integer> map = new HashMap<>();//create a Map object
Integer counter=null;//initalize counter
for(int i=0;i<b.length;i++) { //loop the whole array
counter=map.get(b[i]);//get element from map
if(map.get(b[i]) == null) { //check if it already exists
map.put(b[i], 1);//not exist, add with counter as 1
} else {
counter++;//if already eists, increment the counter & put to Map
map.put(b[i], counter);
}
}
Using simple For loops
public static void main(String[] args) {
String input = "Table is this Table";
String[] arr1 = input.split(" ");
int count = 0;
for (int i = 0; i < arr1.length; i++) {
count = 0;
for (int j = 0; j < arr1.length; j++) {
String temp = arr1[j];
String temp1 = arr1[i];
if (j < i && temp.contentEquals(temp1)) {
break;
}
if (temp.contentEquals(temp1)) {
count = count + 1;
}
if (j == arr1.length - 1) {
System.out.println(">>" + arr1[i] + "<< is present >>" + count + "<< number of times");
}
}
}
}

Counting the number of unique values in a vector

I have a method which takes in parameters in the form of a vector from another vector. This vector can be of the size 2, 3 or 4 elements.
I want to count the frequency of every word in that vector. For example, if the vector contained the strings : "hello", "my" , "hello" , I want to output an array that is
[2, 1] where 2 is the frequency of hello and 1 is the frequency of my.
Here is my attempt after reading a few questions on this website:
int vector_length = query.size();
int [] tf_q = new int [vector_length];
int string_seen = 0;
for (int p = 0; p< query.size(); p++)
{
String temp_var = query.get(p);
for (int q = 0; q< query.size(); q++)
{
if (temp_var == query.get(q) )
{
if (string_seen == 0)
{
tf_q[p]++;
string_seen++;
}
else if (string_seen == 1)
{
tf_q[p]++;
string_seen = 0;
query.remove(p);
}
}
}
}
System.out.print(Arrays.toString(tf_q));
What is the right direction to go?
Use a HashMap of type to track the unique string values you encounter that count each word
String[] vector // your vector
Map<String, Integer> stringMap = new HashMap<String, Integer>();
for (int i = 0; i < vector.length; i++) {
if (stringMap.containsKey(vector[i]) {
Integer wordCount = stringMap.get(vector[i]);
stringMap.put(vector[i], new Integer(wordCount + 1));
}
else {
stringMap.put(vector[i], new Integer(1));
}
}
String[] input = {"Hello", "my", "Hello", "apple", "Hello"};
// use hashmap to track the number of strings
HashMap<String, Integer> map = new HashMap<String, Integer>();
// use arraylist to track the sequence of the output
ArrayList<String> list = new ArrayList<String>();
for (String str : input){
if(map.containsKey(str)){
map.put(str, map.get(str)+1);
} else{
map.put(str, 1);
list.add(str); // if the string never occurred before, add it to arraylist
}
}
int[] output = new int[map.size()];
int index = 0;
for (String str : list){
output[index] = map.get(str);
index++;
}
for (int i : output){
System.out.println(i);
}
This should be your answer! Result is in "int[] output"
If you want to maintain the relation between each word and the frequency of that word, then I suggest that you use a HashMap instead. For example:
Map<String,Integer> histogram = new HashMap<String,Integer>();
for (String word : query)
{
Integer count = histogram.get(word);
if (count == null)
histogram.put(word,1);
else
histogram.put(word,count+1);
}
At this point, you can (for example) print each word with the corresponding frequency:
for (String word : histogram.keySet())
System.out.println(word+" "+histogram.get(word));
Or you can obtain an array which contains only the frequencies, if that's all you want:
Integer[] array = histogram.values().toArray(new Integer[histogram.size()]);
Or even a collection, which is just as useful and convenient as any native array:
Collection<Integer> collection = histogram.values();

Categories