How to Find the Intersection of Two Strings in Java? Last Updated : 12 Feb, 2024 Comments Improve Suggest changes Like Article Like Report We know that the Strings are a collection of characters so if we want to find the intersection of two Strings the simple way is we can compare each character of the first String with the characters of the Second String. Example to Find the Intersection of Two Strings in JavaThere are two strings available s1 and s2 and we want to print the intersection of these two strings in the console. Input: String s1 = "GeeksforGeeks" String s2 = "Geeks" Output: "Geeks" ApproachSo, in the first step we count the frequency of each character present in the string1 here we can use a HashMap which is used to store values in the form of key, and value pair.Now we create a hashmap. and traverse string1 and add each character along with its frequency which means how many times a character has appeared in the string.Now we traverse in String2 and check the same character is present in the hashmap.If the character is present in the hashmap then we decrease the frequency of the character from the hashmap.And we add those characters who have the positive frequency in the new String.Then print it on the console.Below is the implementation of finding the intersection of Two Strings: Java // Java Program for Finding // Intersection of two Strings import java.io.*; import java.util.*; // Driver Class class GFG { public static void main(String[] args) { String s1 = "GeeksforGeeks "; String s2 = "Geeks"; StringBuilder ans = new StringBuilder(); HashMap<Character, Integer> map = new HashMap<>(); // here we store the frequency of each character // present in first string. for (int i = 0; i < s1.length(); i++) { char character = s1.charAt(i); map.put(character, map.getOrDefault(character, 0) + 1); } // here we check the same character present the for (int i = 0; i < s2.length(); i++) { char character = s2.charAt(i); if (map.containsKey(character) && map.get(character) > 0) { ans.append(character); map.put(character, map.get(character) - 1); } } // here we print the output on the console System.out.println("The answer is " + ans); } } OutputThe answer is Geeks Comment More infoAdvertise with us Next Article How to Find the Intersection of Two Strings in Java? devanshuma3v23 Follow Improve Article Tags : Java Java Programs Java-Strings strings Java Examples +1 More Practice Tags : JavaJava-StringsStrings Similar Reads Find the Intersection of Two HashSets in Java HashSets is a type of Collection in Java that cannot contain any duplicate values. It is often used when we need to maintain a unique data set in our application, it uses hashing internally to store the elements in it, so operations like searching, insertion, and deletion take only a constant amount 6 min read How to Find the Intersection and Union of Two PriorityQueues in Java? In Java, PriorityQueue is an implementation of the Queue Interface. It can be used to provide a way to store the elements in a queue based on their priority basis. This means high-priority elements can store first compared to lower-priority elements in the queue. In this article, we will be learning 2 min read How to Find the Longest Common Prefix of Two Strings in Java? In this article, we will find the longest common prefix of two Strings in Java. Examples: Input: String 1= geeksforgeeks, String 2 = geezerOutput: âgeeâ Input: String 1= flower, String 2 = flightOutput: âflâ Methods to Find the Longest common Prefix of two Strings in JavaBelow are the methods by whi 4 min read Java Program to Find the Intersection Between Two Collection Collection means a set of different classes and interfaces are group together into a single unit that has similar functions are called collection and framework we know very that provides a predefined architecture to represents and manipulate collections in java. Here we will be discussing discuss ou 4 min read Java Program For Finding Intersection Point Of Two Linked Lists There are two singly linked lists in a system. By some programming error, the end node of one of the linked lists got linked to the second list, forming an inverted Y-shaped list. Write a program to get the point where two linked lists merge. Above diagram shows an example with two linked lists havi 7 min read Java Program For Finding Intersection Of Two Sorted Linked Lists Given two lists sorted in increasing order, create and return a new list representing the intersection of the two lists. The new list should be made with its own memory â the original lists should not be changed. Example: Input: First linked list: 1->2->3->4->6 Second linked list be 2- 5 min read Java Program For Union And Intersection Of Two Linked Lists Write a java program for a given two Linked Lists, create union and intersection lists that contain union and intersection of the elements present in the given lists. The order of elements in output lists doesn't matter.Example: Input:List1: 10->15->4->20List2: 8->4->2->10Output:In 10 min read Java Program to find the Last Index of a Particular Word in a String To find out the last occurrence of a character or string from a given string across which the substring or character is been searched over and returning the index value of the character or substring is found. Real-life Example: Consider an example of a book. The book contains pages where pages conta 4 min read How to Get Substring Items Within Arraylist in Java? In Java, ArrayList is the pre-defined class of the Java collection framework, and it is part of java.util package. ArrayList can be used to add or remove an element dynamically in a Java program. It can be dynamically based on the elements added or removed from the ArrayList. In this article, we wil 2 min read Java Program to Count the Total Number of Vowels and Consonants in a String Given a String count the total number of vowels and consonants in this given string. Assuming String may contain only special characters, or white spaces, or a combination of all. The idea is to iterate the string and checks if that character is present in the reference string or not. If a character 2 min read Like