![]() |
A string is a data structure in JavaScript that is used to store a set of characters. A string can contain a character, a word, or a sentence. A string also can be empty. It is represented using double quotes, single quotes, or template literals. Examples:Input: str = "abcdabac" We can solve this problem using the methods which are listed below: Table of Content Brute force methodThe idea in this method is to use simple nested loops. By looping through the string starting from first character we will measure the frequency of the character throughout the string. In the next iteration of outer loop we will start from the next character and so on. While doing this we will keep track of the character of maximum frequency till now and also it’s frequency. If the current character will have more frequency then we will update both of them. Example: The below code uses the nested loops to find the maximum frequency character in a string. Javascript
Output
The maximum frequency character in string abcdabac is a The maximum frequency character in string bZzDbc is b Time complexity: O(n2), where n is the length of the string. Space complexity: O(1) Using JavaScript ObjectIn this method, we will iterate through the array values and store each one of them in an JavaScript object as a key and their count as their values, then we iterate through the object to check the maimum frequency character. Example: The below code will explain how to implement above-discussed approach in JavaScript. Javascript
Output
The maximum frequency character in string abcdabac is a The maximum frequency character in string bZzDbc is b Time complexity: O(N+M), where N is the length of given string and M is the number of unique characters in string. Space complexity: O(N) |
Reffered: https://www.geeksforgeeks.org
JavaScript |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 11 |