## Intersection of two strings in Java

intersection of two arrays in java

java intersection of multiple sets

find intersection of two strings c++

get uncommon elements from two lists java 8

remove common elements from two lists java

java set intersection

intersection of two arrays java-8

Need a Java function to find intersection of two strings. i.e. characters common to the strings.

Example:

String s1 = new String("Sychelless"); String s2 = new String("Sydney");

Using `HashSet<Character>`

:

HashSet<Character> h1 = new HashSet<Character>(), h2 = new HashSet<Character>(); for(int i = 0; i < s1.length(); i++) { h1.add(s1.charAt(i)); } for(int i = 0; i < s2.length(); i++) { h2.add(s2.charAt(i)); } h1.retainAll(h2); Character[] res = h1.toArray(new Character[0]);

This is `O(m + n)`

, which is asymptotically optimal.

**Python,** One of the string operations can be computing the intersection of two strings i.e, output the common values that appear in both the strings. There are various Find the intersection of two Strings. but all I found was about finding intersection of Possible duplicate of Intersection of two strings in Java – Tot

Extract the characters

String.toCharArray

Put them in a Set Find the intersection

Set.retainAll

**Sets intersection() function | Guava,** Sets intersection() function | Guava | Java public static void main(String[] args) Set 1: [40, 10, 50, 20, 30] Set 2: [50, 90, 30, 70] Set 1 intersection Set 2: [50, I have two strings in which both have set of numbers and I want the intersection to be the numbers that are common between them. all of them are string. ArrayList<String> list1 = new ArrayList<String> (); list1.add(temp1.get(0)); ArrayList<String> list2 = new ArrayList<String> (); list2.add(temp1.get(1)); ArrayList<String> intersection = new ArrayList<String> (list1); intersection.retainAll(list2); System.out.println ( "Charstic sets Intersection ::: "+intersection );

Most basic approach:

String wordA = "Sychelless"; String wordB = "Sydney"; String common = ""; for(int i=0;i<wordA.length();i++){ for(int j=0;j<wordB.length();j++){ if(wordA.charAt(i)==wordB.charAt(j)){ common += wordA.charAt(i)+" "; break; } } } System.out.println("common is: "+common);

**How to get intersection between two arrays,** Java program to get the intersection between two string arrays and print the output. Main.java. import java.util.Arrays;. import Like many other things, this has become much easier thanks to the introduction of streams in Java 8. 2. Intersection of Two Lists of Strings Let's create two List s of String s with some intersection — both having some duplicated elements:

More detail on saugata's response (appeared while I was writing this): -

public static void main(String[] args) { String s1 = "Seychelles"; String s2 = "Sydney"; Set<Character> ss1 = toSet(s1); ss1.retainAll(toSet(s2)); System.out.println(ss1); } public static Set<Character> toSet(String s) { Set<Character> ss = new HashSet<Character>(s.length()); for (char c : s.toCharArray()) ss.add(Character.valueOf(c)); return ss; }

**Intersection of Two Lists in Java,** Let's create two Lists of Strings with some intersection — both having some duplicated elements: ? 1. 2. List<String> list = Arrays.asList Learn to find the intersection between two arrays in Java using HashSet class. An intersection is a group of common items that belong to two different sets.. Intersection. To get the intersection of two arrays, follow these steps:

I think the algorithm you are looking for is the problem of the longest common subsequence

**Get the intersection of two sets in Java,** To get the intersection of two sets, use the retainAll() method. Here are out two set: First set − HashSet <String> set1 = new HashSet <String>(); In addition, the small size of these sets makes finding the intersection very quick. If the intersection of the two sets is empty, we print NO on a new line; if the intersection of the two sets is not empty, then we know that strings and share one or more common characters and we print YES on a new line. 1 2

**Intersection - House of Codes,** A compilation of 100 Java(Interview)Programming problems which have been solved . I have given 3)Find the intersection of two given string. To find the intersection of two arrays in java use two loops. The outer loop is to iterate the elements of the first array whereas, the second loop is to iterate the elements of the second array. Within the second loop compare the elements of the two arrays: Example. Live Demo

**Determining the existence of string Intersections,** Returns true if there are any such substrings which appear in both strings. Compute this in linear time using a HashSet. Solution: import java.util. InterviewQuestions, Java, programming. Given two sorted arrays, Write a java code to find intersection of two arrays. For example, if the input arrays are: arr1 [] = {2, 3, 6, 7, 9, 11} arr2 [] = {4, 6, 8, 9, 12} Then your program should print intersection as {6, 9}.

**Determine if two strings share a common sub-string.,** Question: Given two strings, determine if they share a common sub-string. If the intersection of the two sets is empty, we print NO on a new line; if the intersection of the two sets is not empty, then HashSet; import java.util. Intersection of two Lists. 1. Java 8 Stream API. From Java 8 you can use sequential stream on list1 (bigger one) and filter it to produce a stream containing matching elements of specified collection ( list2) and use Collectors.toList () to accumulate the intersects in to new List.

##### Comments

- what is O(m+n) and how do u arrive at that complexity and what do mean by asymptotically optimal
`m`

and`n`

are the lengths of the strings. Adding, removing, and checking a HashSet item are all O(1). So it's basically m (add) + n (add) + m (one check for every element in h1) + m (up to m removals) + m (converting to character array). So that's 4m + n, which is O(m + n) (the constant drops out). O is Big O. Asymptotically optimal means that there is no algorithm with a smaller big O.- The "O(m + n)" stuff is used for analyzing the time and space requirements of algorithms. See: en.wikipedia.org/wiki/Big_O_notation
- @MonaJalal, it's just required so that toArray returns an array of type Character. This is a consequence of Java's somewhat incomplete generic system (it was added on later, so they couldn't do everything a new system like C# could). See docs.oracle.com/javase/7/docs/api/java/util/… .
- This approach of set fails for following input "aa","aa" .
- Output from this is
`"common is: S y e e "`

. I would not expect a correct solution to include duplicates (at least not if both words don't have the letters multiple times). - This is
`O(m * n)`

because of the nested loop. Separately, you can easily make the String creation more efficient with a`StringBuilder`

, since you know the upper bound of the size ahead of time. Rasmus is also right that the intersection should be a set (no duplicates). - The duplication can be fixed very easy adding a condition one line before appending to the "common" String like: if(!common.contains(wordA.charAt(i)+""))
- the solution is posted by above members
- This is for c# though, but the algorithm is the same
- how it will give common chars
- It gives substring and I am 99% sure that it is what he needs.
- I need common characters