Question

In: Computer Science

Sort a string array by frequency given an array of string write a function that will...

Sort a string array by frequency

given an array of string write a function that will return an array that is sorted by frequency

example

{"hello","hola","hello","hello","ciao","hola","hola","hola"}

returned array should be

{"hola","hello","ciao"}

Solutions

Expert Solution

CODE IN JAVA:

Sort.java file:

import java.util.*;
public class Sort {
   public static String[] sortByFrequency(String arr[]) {
       ArrayList<String> uniqueList = new ArrayList<String>();
       for(int i=0;i<arr.length;i++) {
           if(uniqueList.indexOf(arr[i])==-1)
               uniqueList.add(arr[i]);
       }
       int n = uniqueList.size();
       int countWords[]= new int[n];
       int count,i=0;
       for(String word:uniqueList) {
           count = 0 ;
           for(int j=0;j<arr.length;j++) {
               if(arr[j].equals(word))
                   count += 1 ;
           }
           countWords[i] = count ;
           i+=1;
       }
       String uniqueWords[] = new String[n];
       i=0;
       for(String word:uniqueList) {
           uniqueWords[i] = word;
           i+=1;
       }
       //sorting the array of strings by frequency
       for(i=0;i<n;i++) {
           for(int j=0;j<n-1;j++) {
               if(countWords[j]<countWords[j+1]) {
                   String temp = uniqueWords[j];
                   uniqueWords[j] = uniqueWords[j+1];
                   uniqueWords[j+1] = temp ;
                   int temp1 = countWords[j];
                   countWords[j] = countWords[j+1];
                   countWords[j+1] = temp1 ;
               }
           }
       }
       return uniqueWords;
   }
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       String arr[] = {"hello","hola","hello","hello","ciao","hola","hola","hola"};
       System.out.println("The array is :");
       System.out.println(arr);
      
       for(String word:arr)
           System.out.print(" "+word+" ");
       System.out.println("");
       System.out.println("After sorting by the frequency the array is:");
       String sortedArr[] = sortByFrequency(arr);
       for(String word:sortedArr)
           System.out.print(" "+word+" ");
   }

}
OUTPUT:


Related Solutions

Write a program in Java to sort the given array using merge sort, quick sort, insertion...
Write a program in Java to sort the given array using merge sort, quick sort, insertion sort, selection sort and bubble sort based on the input from the user which sorting technique they wanted to use. Get the array size, array elements from the user, and also display the sorted array along with the name of the sorting technique used.
Given the following array, write a program in C++ to sort the array using a selection...
Given the following array, write a program in C++ to sort the array using a selection sort and display the number of scores that are less than 500 and those greater than 500. Scores[0] = 198 Scores[3] = 85 Scores[6] = 73 Scores[9] = 989 Scores[1] = 486 Scores[4] = 216 Scores[7] = 319 Scores[2] = 651 Scores[5] = 912 Scores[8] = 846
Write a function to sort data (in increasing order) in an array using a. pass by...
Write a function to sort data (in increasing order) in an array using a. pass by value b. and pass by reference.
Write a function that takes an Array of cases, as well as an FSA String value....
Write a function that takes an Array of cases, as well as an FSA String value. This Case data is an Array where each item in the Array is a custom Case Object. The Array is really [case1, case2, case3, ...], and each Case Object has the following structure:   {      "Age Group": "40 to 49 Years",      "Neighbourhood Name": "Annex",      "Outcome": "RESOLVED",      "Client Gender": "FEMALE",      "Classification": "CONFIRMED",      "FSA": "M5R",      "Currently Hospitalized": "No",      "Episode Date": "2020-09-12",      "Assigned_ID": 17712,      "Outbreak Associated": "Sporadic",      "Ever...
Write a function in JAVASCRIPT that accepts two arguments (a string array and a character). The...
Write a function in JAVASCRIPT that accepts two arguments (a string array and a character). The function will check each character of the strings in the array and removes the character, regardless of case. If the first letter of the string is removed the next letter must be capitalized. Your function would return the result as one string, where each element of array is separated by comma. E.G. function([“John”,”Hannah”,”Saham”], “h”); // returns ‘Jon,Anna,Saam Use of any built in string function...
Sorting (merge) Sort the array (as in Part 2) using merge sort. Write the array content...
Sorting (merge) Sort the array (as in Part 2) using merge sort. Write the array content after each pass (i.e., from pass 1 to pass 9). Each pass is defined as the completion of one merge operation. Suppose an array contains the following integers: -1, 20, 10, -5, 0, -7, 100, -7, 30, -10. Sort the array using the following algorithms: selection sort, bubble sort, and insertion sort. For each algorithm, write the array content after each pass (i.e., from...
Write a MIPS program that uses an implementation of quick sort to sort an array of...
Write a MIPS program that uses an implementation of quick sort to sort an array of numbers (Translate the following code into (Mars Assembly language). Quicksort Implementation - C int partition(int arr [] , int left , int right) { int i=left, j=right; int tmp; int pivot = arr[(left + right) / 2]; while (i <= j) { while (arr [ i ] < pivot) i ++; while (arr [ j ] > pivot) j −−; if (i <= j)...
Write code for A counting sort is a technique to sort an array of n positive...
Write code for A counting sort is a technique to sort an array of n positive integers that lie between 0 and m, inclusive. You need m + 1 counters. Then, making only one pass through the array, you count the number of times each integer occurs in the array. For example, the figure below shows an array of integers that lie between 0 and 4 and the five counters after a counting sort has made its pass through the...
C Write a function int sort(int** arr, int n) that takes as input an array of...
C Write a function int sort(int** arr, int n) that takes as input an array of int pointers, multiplies the ints the pointers point to by -1, and then sorts the array such that the values pointed to by the pointers are in decreasing order. For example, if the array is size 10 the pointer at index 0 will point to the largest value after multiplying by -1 and the pointer at index 9 will point to the smallest value...
Sorting (quick) Sort the array using quick sort. Write the array content after each pass (i.e.,...
Sorting (quick) Sort the array using quick sort. Write the array content after each pass (i.e., from pass 1 to pass 7). Each pass is defined as the completion of one partition. We always pick the first array element as the pivot for each partition.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT