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 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 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)...
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...
Given an array of integers and the size of the array, write a function findDuplicate which prints the duplicate element from the array.
C++ Programming using iostream and namespace stdGiven an array of integers and the size of the array, write a function findDuplicate which prints the duplicate element from the array. The array consists of all distinct integers except one which is repeated. Find and print the repeated number. If no duplicate is found, the function should print -1. void findDuplicate (int [ ], int)Example 1: Given array: {2,3,5,6,11,20,4,8,4,9} Output: 4 Example 2: Given array: {1,3,5,6,7,8,2,9} Output: -1
C# Palindrome Permutation: Given a string, write a function to check if it is a permutation...
C# Palindrome Permutation: Given a string, write a function to check if it is a permutation of a palindrome. A palindrome is a word or phrase that is the same forwards and backwards. A permutation is a rearrangement of letters. The palindrome does not need to be limited to just dictionary words. Input: Tact Coa Output: True (permutations: "taco cat", "atco cta", etc.) Comment your code to explain it.
Write a recursive method to determine if a String is a palindrome. Create a String array...
Write a recursive method to determine if a String is a palindrome. Create a String array with several test cases and test your method. Write a recursive method to determine if a String is a palindrome. Create a String array with several test cases and test your method. In Java
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array...
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array and remove the duplicates in-place such that each element appears only once in the input array and returns the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Find the time complexity of your removeDuplicates() method in Big-O notation and write that in a comment line on the top...
Project: Given a string s and an integer array indices of the same length. The string...
Project: Given a string s and an integer array indices of the same length. The string s will be shuffled such that the character at the i th position moves to indices[i] in the shuffled string. Return the shuffled string. Example: Input: s = "codeleet", indices = [4,5,6,7,0,2,1,3] Output: "leetcode" Explanation: As shown, "codeleet" becomes "leetcode" after shuffling. You need to do: Create a class called ShuffledStringApp. Keeping class StringOperation and IO in util package. In this project, you will...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT