Algorithms

Create a Data Structure GetMostFrequent of O(1)

&NewLine;<p class&equals;"wp-block-paragraph">In this article&comma; we will discuss how to create a Data Structure with Insert&comma; Delete and GetMostFrequent operations of O&lpar;1&rpar;&period; This Data Structure will proform Insert&comma; delete&comma; and get the most frequent item in the constant time&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">As we know that the insertion and deletion will be performed on a hash map with the constant time O&lpar;1&rpar;&period; So&comma; a hash map will be the right choice for insertion and deletion&period; But&comma; how to implement get most frequent method with O&lpar;1&rpar; time&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<div class&equals;"wp-block-image"><figure class&equals;"aligncenter"><img src&equals;"https&colon;&sol;&sol;i0&period;wp&period;com&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2019&sol;10&sol;Get-Most-Frequent-Data-Structure&period;jpeg&quest;fit&equals;530&percnt;2C398&amp&semi;ssl&equals;1" class&equals;"wp-image-4496"&sol;><figcaption><strong>Creating Data Structure with Insert&comma; Delete and GetMostFrequent of O&lpar;1&rpar;<&sol;strong><&sol;figcaption><&sol;figure><&sol;div>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">Let&&num;8217&semi;s see the following implementation of this method to achieve get the most frequent item with O&lpar;1&rpar; time&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<blockquote class&equals;"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>Other Data Structures Articles<&sol;p><cite>1&period; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;implement-lru-cache-algorithm-in-java&sol;"><strong>Create a LRU cache<&sol;strong><&sol;a><br>2&period; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;least-frequently-used-lfu-cache-implementation&sol;"><strong>Create a LFU cache<&sol;strong><&sol;a><&sol;cite><&sol;blockquote>&NewLine;&NewLine;&NewLine;&NewLine;<h2 class&equals;"wp-block-heading">Create a Data Structure with Insert&comma; Delete and GetMostFrequent of O&lpar;1&rpar;<&sol;h2>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">We will get the solution for this problem by using HashMap and LinkedList&period; Let&&num;8217&semi; see the following code&colon;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-code"><code>&sol;&ast;&ast;&NewLine; &ast; &NewLine; &ast;&sol;&NewLine;package com&period;dineshonjava&period;algo&period;gmf&semi;&NewLine;&NewLine;import java&period;util&period;HashSet&semi;&NewLine;import java&period;util&period;Set&semi;&NewLine;&NewLine;&sol;&ast;&ast;&NewLine; &ast; &commat;author Dinesh&period;Rajput&NewLine; &ast;&NewLine; &ast;&sol;&NewLine;public class Node &lbrace;&NewLine;&Tab;&NewLine;&Tab;int value&semi;&NewLine;&Tab;Node prev&semi;&NewLine;&Tab;Node next&semi;&NewLine;&Tab;Set&lt&semi;Integer> set&semi;&NewLine; &NewLine;&Tab;public Node&lpar;int v&rpar;&lbrace;&NewLine;&Tab;&Tab;value &equals; v&semi;&NewLine;&Tab;&Tab;set &equals; new HashSet&lt&semi;Integer>&lpar;&rpar;&semi;&NewLine;&Tab;&rcub;&NewLine;&rcub;&NewLine;<&sol;code><&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">We will use the <em>LinkedList <&sol;em>to hold the frequency of the item in the data structure along with the item stored in the <em>HashSet<&sol;em>&period; This set will contain all items with the same frequency&period; Let&&num;8217&semi;s see the other class of our Data structure&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-code"><code>&sol;&ast;&ast;&NewLine; &ast; &NewLine; &ast;&sol;&NewLine;package com&period;dineshonjava&period;algo&period;gmf&semi;&NewLine;&NewLine;import java&period;util&period;HashMap&semi;&NewLine;import java&period;util&period;Map&semi;&NewLine;&NewLine;&sol;&ast;&ast;&NewLine; &ast; &commat;author Dinesh&period;Rajput&NewLine; &ast;&NewLine; &ast;&sol;&NewLine;public class GetMostFrequent &lbrace;&NewLine;&Tab;&NewLine;&Tab;Map&lt&semi;Integer&comma; Node> map&semi;&NewLine;&Tab;Node tail&comma; head&semi;&NewLine;&Tab;&NewLine;&Tab;&sol;&ast;&ast; Initialize your data structure here&period; &ast;&sol;&NewLine;&Tab;public GetMostFrequent&lpar;&rpar; &lbrace;&NewLine;&Tab;&Tab;super&lpar;&rpar;&semi;&NewLine;&Tab;&Tab;map &equals; new HashMap&lt&semi;>&lpar;&rpar;&semi;&NewLine;&Tab;&rcub;&NewLine;&Tab;&NewLine;&Tab;&sol;&ast;&ast; Adding items to your data structure&period; &ast;&sol;&NewLine;&Tab;public void add&lpar;int val&rpar; &lbrace;&NewLine;&Tab;&Tab;&NewLine;&Tab;&Tab;if&lpar;map&period;containsKey&lpar;val&rpar;&rpar; &lbrace;&NewLine;&Tab;&Tab;&Tab;Node node &equals; map&period;get&lpar;val&rpar;&semi;&NewLine;&Tab;&Tab;&Tab;node&period;set&period;remove&lpar;val&rpar;&semi;&NewLine;&Tab;&Tab;&Tab;if&lpar;node&period;next &excl;&equals; null&rpar; &lbrace;&NewLine;&Tab;&Tab;&Tab;&Tab;node&period;next&period;set&period;add&lpar;val&rpar;&semi;&NewLine;&Tab;&Tab;&Tab;&Tab;map&period;put&lpar;val&comma; node&period;next&rpar;&semi;&NewLine;&Tab;&Tab;&Tab;&rcub;else &lbrace;&NewLine;&Tab;&Tab;&Tab;&Tab;Node newnode &equals; new Node&lpar;node&period;value&plus;1&rpar;&semi;&NewLine;&Tab;&Tab;&Tab;&Tab;newnode&period;set&period;add&lpar;val&rpar;&semi;&NewLine;&Tab;&Tab;&Tab;&Tab;map&period;put&lpar;val&comma; newnode&rpar;&semi;&NewLine;&Tab;&Tab;&Tab;&Tab;node&period;next &equals; newnode&semi;&NewLine;&Tab;&Tab;&Tab;&Tab;newnode&period;prev &equals; node&semi;&NewLine;&Tab;&Tab;&Tab;&Tab;tail &equals; newnode&semi;&NewLine;&Tab;&Tab;&Tab;&rcub;&NewLine;&Tab;&Tab;&rcub;else &lbrace;&NewLine;&Tab;&Tab;&Tab;if&lpar;head &excl;&equals; null &amp&semi;&amp&semi; tail &excl;&equals; null&rpar; &lbrace;&NewLine;&Tab;&Tab;&Tab;&Tab;if&lpar;tail&period;value > 1&rpar; &lbrace;&NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;head&period;set&period;add&lpar;val&rpar;&semi;&NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;map&period;put&lpar;val&comma; head&rpar;&semi;&NewLine;&Tab;&Tab;&Tab;&Tab;&rcub;else &lbrace;&NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;tail&period;set&period;add&lpar;val&rpar;&semi;&NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;map&period;put&lpar;val&comma; tail&rpar;&semi;&NewLine;&Tab;&Tab;&Tab;&Tab;&rcub;&NewLine;&Tab;&Tab;&Tab;&rcub;else &lbrace;&NewLine;&Tab;&Tab;&Tab;&Tab;Node node &equals; new Node&lpar;1&rpar;&semi;&NewLine;&Tab;&Tab;&Tab;&Tab;node&period;set&period;add&lpar;val&rpar;&semi;&NewLine;&Tab;&Tab;&Tab;&Tab;map&period;put&lpar;val&comma; node&rpar;&semi;&NewLine;&Tab;&Tab;&Tab;&Tab;head &equals; node&semi;&NewLine;&Tab;&Tab;&Tab;&Tab;tail &equals; node&semi;&NewLine;&Tab;&Tab;&Tab;&rcub;&NewLine;&Tab;&Tab;&rcub;&NewLine;&Tab;&rcub;&NewLine;&Tab;&NewLine;&Tab;&sol;&ast;&ast; Getting most frequent item from your data structure&period; &ast;&sol;&NewLine;&Tab;public int getMostFrequent&lpar;&rpar; &lbrace;&NewLine;&Tab;&Tab;&NewLine;&Tab;&Tab;if&lpar;tail &equals;&equals; null&rpar;&NewLine;&Tab;&Tab;&Tab;return -1&semi;&NewLine;&Tab;&Tab;&NewLine;&Tab;&Tab;return tail&period;set&period;iterator&lpar;&rpar;&period;next&lpar;&rpar;&semi;&NewLine;&Tab;&rcub;&NewLine;&Tab;&NewLine;&Tab;&sol;&ast;&ast; Removing item from your data structure&period; &ast;&sol;&NewLine;&Tab;public void remove&lpar;int val&rpar; &lbrace;&NewLine;&Tab;&Tab;&NewLine;&Tab;&Tab;if&lpar;map&period;containsKey&lpar;val&rpar;&rpar; &lbrace;&NewLine;&Tab;&Tab;&Tab;Node node &equals; map&period;get&lpar;val&rpar;&semi;&NewLine;&Tab;&Tab;&Tab;node&period;set&period;remove&lpar;val&rpar;&semi;&NewLine;&Tab;&Tab;&Tab;&NewLine;&Tab;&Tab;&Tab;if&lpar;node&period;value &equals;&equals; 1&rpar;&lbrace;&NewLine;&Tab;&Tab;&Tab;&Tab;map&period;remove&lpar;val&rpar;&semi;&NewLine;&Tab;&Tab;&Tab;&rcub;else&lbrace;&NewLine;&Tab;&Tab;&Tab;&Tab;node&period;prev&period;set&period;add&lpar;val&rpar;&semi;&NewLine;&Tab;&Tab;&Tab;&Tab;map&period;put&lpar;val&comma; node&period;prev&rpar;&semi;&NewLine;&Tab;&Tab;&Tab;&rcub;&NewLine;&Tab;&Tab;&Tab;&NewLine;&Tab;&Tab;&Tab;while&lpar;tail &excl;&equals; null &amp&semi;&amp&semi; tail&period;set&period;size&lpar;&rpar; &equals;&equals; 0&rpar;&lbrace;&NewLine;&Tab;&Tab;&Tab;&Tab;tail &equals; tail&period;prev&semi;&NewLine;&Tab;&Tab;&Tab;&rcub;&NewLine;&Tab;&Tab;&Tab;&NewLine;&Tab;&Tab;&rcub;else &lbrace;&NewLine;&Tab;&Tab;&Tab;System&period;out&period;println&lpar;"No such item exist&excl;&excl;&excl;"&rpar;&semi;&NewLine;&Tab;&Tab;&rcub;&NewLine;&Tab;&rcub;&NewLine;&rcub;&NewLine;<&sol;code><&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">The above <em>GetMostFrequent <&sol;em>our collection data structure class&comma; it has three methods <em>add&lpar;&rpar;<&sol;em>&comma; <em>remove&lpar;&rpar;<&sol;em>&comma; and <em>getMostFrequent&lpar;&rpar;&period; All these methods have<&sol;em> time complexity of O&lpar;1&rpar;&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">Let&&num;8217&semi;s create a test class for this data structure as the following&colon;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-code"><code>&sol;&ast;&ast;&NewLine; &ast; &NewLine; &ast;&sol;&NewLine;package com&period;dineshonjava&period;algo&period;gmf&semi;&NewLine;&NewLine;&sol;&ast;&ast;&NewLine; &ast; &commat;author Dinesh&period;Rajput&NewLine; &ast;&NewLine; &ast;&sol;&NewLine;public class GMFTest &lbrace;&NewLine;&NewLine;&Tab;&sol;&ast;&ast;&NewLine;&Tab; &ast; &commat;param args&NewLine;&Tab; &ast;&sol;&NewLine;&Tab;public static void main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace;&NewLine;&Tab;&Tab;&NewLine;&Tab;&Tab;&sol;&sol;Creating your data structure object&NewLine;&Tab;&Tab;GetMostFrequent frequent &equals; new GetMostFrequent&lpar;&rpar;&semi;&NewLine;&Tab;&Tab;&NewLine;&Tab;&Tab;&sol;&sol;Adding items to your data structure with O&lpar;1&rpar;&NewLine;&Tab;&Tab;frequent&period;add&lpar;2&rpar;&semi;&NewLine;&Tab;&Tab;frequent&period;add&lpar;3&rpar;&semi;&NewLine;&Tab;&Tab;frequent&period;add&lpar;4&rpar;&semi;&NewLine;&Tab;&Tab;frequent&period;add&lpar;5&rpar;&semi;&NewLine;&Tab;&Tab;frequent&period;add&lpar;2&rpar;&semi;&NewLine;&Tab;&Tab;frequent&period;add&lpar;6&rpar;&semi;&NewLine;&Tab;&Tab;frequent&period;add&lpar;3&rpar;&semi;&NewLine;&Tab;&Tab;frequent&period;add&lpar;3&rpar;&semi;&NewLine;&Tab;&Tab;frequent&period;add&lpar;2&rpar;&semi;&NewLine;&Tab;&Tab;frequent&period;add&lpar;2&rpar;&semi;&NewLine;&Tab;&Tab;&NewLine;&Tab;&Tab;&sol;&sol;Getting most frequent item from your data structure with O&lpar;1&rpar;&NewLine;&Tab;&Tab;System&period;out&period;println&lpar;"Most frequent number in the collection is "&plus;frequent&period;getMostFrequent&lpar;&rpar;&rpar;&semi; &sol;&sol;Expected 2&NewLine;&Tab;&Tab;&NewLine;&Tab;&Tab;&sol;&sol;Removing item from your data structure with O&lpar;1&rpar;&NewLine;&Tab;&Tab;frequent&period;remove&lpar;2&rpar;&semi;&NewLine;&Tab;&Tab;frequent&period;remove&lpar;2&rpar;&semi;&NewLine;&Tab;&Tab;&NewLine;&Tab;&Tab;System&period;out&period;println&lpar;"Most frequent number in the collection is "&plus;frequent&period;getMostFrequent&lpar;&rpar;&rpar;&semi; &sol;&sol;Expected 3&NewLine;&Tab;&Tab;&NewLine;&Tab;&rcub;&NewLine;&NewLine;&rcub;&NewLine;<&sol;code><&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">Let&&num;8217&semi;s run this test class and see the following output&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-code"><code>Most frequent number in the collection is 2&NewLine;Most frequent number in the collection is 3<&sol;code><&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph"><&sol;p>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;least-frequently-used-lfu-cache-implementation&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '4495'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design PatternsĀ using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago