Why ArrayList is faster than HashSet in java? - java

I used to thing that HashSet is a pretty fast data structure implementation because it uses hashes (and is implemented via HashMap in its turn).
I was solving some problems and decided to check performance issue, so here it is:
You are given an array with numbers - [11, 3, 11, 11, 3, 2, 0, -2, 2]
You are supposed to write a function that returns the number that appears "odd" number of times.
Here is my solution:
public class OddNumInArray {
public static List<Integer> oddNumList(int [] ar){
Collection <Integer> l = new ArrayList<>();
for (int n: ar) {
if (l.contains(n)) {
l.remove(n);
}
else {
l.add(n);
}
}
return (List) l;
}
public static Set<Integer> oddNumHSet(int [] ar){
Set <Integer> l = new HashSet<>();
for (int n: ar) {
if (l.contains(n)) {
l.remove(n);
}
else {
l.add(n);
}
}
return l;
}
public static void main(String [ ]arg) {
int [] a1 = new int [10000000];
for (int i=0; i<10; i++){
a1[i]=(new Random()).nextInt(5);
}
long cur= System.nanoTime();
System.out.println(oddNumList(a1));
long c1 = System.nanoTime()-cur;
System.out.println("TIME CONSUMED:" +c1);
cur= System.nanoTime();
System.out.println(oddNumHSet(a1));
long c2 = System.nanoTime()-cur;
System.out.println("TIME CONSUMED:" + c2);
System.out.println("c1/c2*100: "+ (new Double(c1)/new Double(c2)*100));
}
}
And here is an output:
[1, 0]
TIME CONSUMED:101804000
[0, 1]
TIME CONSUMED:183261000
c1/c2*100: 55.55137208680516
So, why is implementation with ArrayList is quicker than one with HashSet by almost 2 times?
Thank you.

ArrayList doesn't have code to check for duplicates. So, it just adds elements as and how you try to add them. A HashSet on the other hand is meant to have only unique elements, so it makes a check to prevent insertion of duplicate elements.

Related

How to subtract two int arrays in Java?

I have this array in Java:
private int[] all = new int [] {0,1,2,3,4,5};
I want to set a new var that equal to all-{2,3,5} (equals {0,1,4})
Is there any build-in function that performs subtraction between two arrays?
You can code up set subtraction using Java 8 streams.
The idea is to construct a hash set of integers that you would like to exclude, and then run through the original array, check each element against the exclusion set in O(1), and keep only elements that should not be excluded.
int[] all=new int [] {0,1,2,3,4,5};
int[] ex=new int [] {2,3,5};
Set<Integer> exSet = IntStream.of(ex).boxed().collect(Collectors.toCollection(HashSet::new));
int[] res = IntStream.of(all).filter(val -> !exSet.contains(val)).toArray();
Demo.
I understood what you want to say and figured out a way to do that , what exactly you want.
If you use ArrayList then you could use removerAll() to remove or subtract.
Lets see this on the working code
import java.util.ArrayList;
public class removeAllInArrayList {
public static void main(String[] args) {
ArrayList <Integer> numbers = new ArrayList<Integer>();
numbers.add(0);
numbers.add(1);
numbers.add(2);
numbers.add(3);
numbers.add(4);
numbers.add(5);
System.out.print("The number array contains:- \t");
display (numbers);
ArrayList <Integer> numbers2 = new ArrayList<Integer>();
numbers2.add(0);
numbers2.add(2);
numbers2.add(4);
numbers2.add(6);
System.out.print("The number2 array contains:- \t ");
display(numbers2);
System.out.println("Subtract number2 from number.");
numbers.removeAll(numbers2);
System.out.print("Now, number array contains:- \t");
display (numbers);
}
public static void display (ArrayList array){
for (int i =0 ; i < array.size() ; i++){
System.out.print(array.get(i) + " ");
}
System.out.println ();
}
}
The result of the following code is
The number array contains:- 0 1 2 3 4 5
The number2 array contains:- 0 2 4 6
Subtract number2 from number.
Now, number array contains:- 1 3 5
Hope this works , and helps to solve out the code.
I don't think there is such a build-in method. You can try to find some library which can do that, or write your own method:
public static int[] removeEqual(int[] a_arr, int[] b_arr) {
List<Integer> list = new ArrayList<>();
for (int a : a_arr) { list.add(a); }
Iterator<Integer> iter = list.iterator();
while(iter.hasNext()) {
int a = iter.next();
for (int b : b_arr) {
if (a == b) {iter.remove();}
}
}
int[] result = new int[list.size()];
for (int i = 0; i < list.size(); i++) {result[i] = list.get(i);}
return result;
}
Test
public static void main(String[] args) {
System.out.println("expected: [0, 1, 4], actual: " +
Arrays.toString(removeEqual(new int[] {0,1,2,3,4,5}, new int[] {2, 3, 5})));
}
Output: expected: [0, 1, 4], actual: [0, 1, 4]
You can do what you want with lists:
List<Integer> all = new ArrayList<>(Arrays.asList(0, 1, 2, 3, 4, 5));
List<Integer> toRemove = Arrays.asList(2, 3, 5);
all.removeAll(toRemove);
System.out.println(all); // [0, 1, 4]
In order to be able to remove elements from the all list, you should create it with the new ArrayList<>(anotherList) constructor, since the lists returned by Arrays.asList cannot be structurally modified.
I tested Streams vs Collections:
public static void main(String... args)
{
Random rand = new Random();
int[] all = new int[100000];
int[] ex = new int[50000];
fillArrays(rand, all, ex);
long start = System.currentTimeMillis();
testWithStreams(all, ex);
System.out.println(String.format("Streams: %d", System.currentTimeMillis() - start));
fillArrays(rand, all, ex);// Avoid any external optimization
start = System.currentTimeMillis();
testWithCollections(all, ex);
System.out.println(String.format("Collections: %d", System.currentTimeMillis() - start));
fillArrays(rand, all, ex);// Avoid any external optimization
start = System.currentTimeMillis();
testWithCollectionsToArray(all, ex);
System.out.println(String.format("Collections -> Array: %d", System.currentTimeMillis() - start));
fillArrays(rand, all, ex);// Avoid any external optimization
start = System.currentTimeMillis();
testWithStreamsAndSet(all, ex);
System.out.println(String.format("Streams + HashSet: %d", System.currentTimeMillis() - start));
}
private static void fillArrays(Random rand, int[] all, int[] ex)
{
for (int i = 0; i < all.length; i++)
{
all[i] = rand.nextInt();
}
for (int i = 0; i < ex.length; i++)
{
ex[i] = all[rand.nextInt(all.length)];
}
// System.out.println(String.format("all values: %d, %d, %d", all[0], all[1], all[2]));
// System.out.println(String.format("ex values: %d, %d, %d", ex[0], ex[1], ex[2]));
}
private static int[] testWithStreams(int[] all, int[] ex)
{
return Arrays.stream(all).filter(
(elementOfAll) -> Arrays.stream(ex).noneMatch(
(elementOfEx) -> elementOfAll == elementOfEx
)
).toArray();
}
private static List<Integer> testWithCollections(int[] all, int[] ex)
{
List<Integer> listOfAll = Arrays.stream(all).boxed().collect(Collectors.toList());
listOfAll.removeAll(Arrays.stream(ex).boxed().collect(Collectors.toList()));
return listOfAll;
}
private static int[] testWithCollectionsToArray(int[] all, int[] ex)
{
return testWithCollections(all, ex).stream().mapToInt((v) -> v).toArray();
}
private static int[] testWithStreamsAndSet(int[] all, int[] ex)
{
HashSet<Integer> exSet = Arrays.stream(ex).boxed().collect(Collectors.toCollection(HashSet::new));
return Arrays.stream(all).filter((element) -> !exSet.contains(element)).toArray();
}
The Output:
Streams: 13823
Collections: 2905
Collections -> Array: 2931
Streams + HashSet: 29
EDIT:
Added the example of dasblinkenlight

return duplicates from an array in efficient way in java [duplicate]

This question already has answers here:
Java Array, Finding Duplicates
(17 answers)
Closed 8 years ago.
I want to return duplicates in an array.
int[] strArray = new int[] {1,1, 2, 3, 2, 2, 3};
I have used below method to return duplicates.
private static Set<Integer> checkDuplicate(int[] intArray) {
Set<Integer> values = new HashSet<>();
for (int i = 0; i < intArray.length - 1; i++) {
if (intArray[i] == (intArray[i + 1])) {
values.add(intArray[i]);
}
else
System.out.println("not equal");
}
return values;
}
But in this way it checks only the consequtive values.And this needs huge comparisons and time consuming. So is there any better way to do this?
If you do not want to use hashing (HashSet or HashMap), you can first sort the array. And then, you can find duplicates by checking consecutive values. Overall, this method has O(n log n) time complexity.
You may try this example:
import java.util.*;
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
int[] strArray = new int[] {1,1, 2, 3, 2, 2, 3, 4, 7, 5, 4};
Set<Integer> duplicates = checkDuplicate(strArray);
System.out.printf("Duplicates: %s\n", duplicates);
}
private static Set<Integer> checkDuplicate(int[] intArray)
{
Set<Integer> duplicates = new HashSet<Integer>();
Set<Integer> tmp = new HashSet<Integer>();
for(Integer i: intArray)
{
if(!tmp.add(i))
{
duplicates.add(i);
}
}
return duplicates;
}
}
If efficiency is what you are looking for then
use HashMap that it has O(1) speed, Also iterate the array of integers in enhanced forloop because it is slightly faster than ordinary forloop
private static Set<Integer> checkDuplicate(int[] intArray) {
HashMap<Integer,Integer> values = new HashMap<Integer,Integer>();
Set<Integer> values2 = new HashSet<Integer>();
for(Integer i : intArray) //0(n)
{
if(values.get(i) != null) //O(1)
values2.add(i);
else
values.put(i, i);
}
return values2;
}
Scan your input and store each number with it's count in a Map<Interger, Integer>. Then loop over the Map and put all keys with value>1 in the resulting Set
See also here: https://stackoverflow.com/a/15217535/461499
You must have to use Collections.frequency
It uses only equals method. If you use any collection Map,Set,List which uses equals method to compare two objects as well as Has collection uses hashCode methods which takes more processing time.
public static void main(String[] args) {
int[] arr = { 1, 2, 3, 1, 4, 4, 1, 5 };
// System.out.println(Arrays.toString(arr));
List<Integer> l = new ArrayList<Integer>();
for (int i = 0; i < arr.length; i++) {
l.add(i, arr[i]);
}
// System.out.println(l);
Set<Integer> set = new HashSet<Integer>();
for (int j = 0; j < l.size(); j++) {
if (Collections.frequency(l, l.get(j)) > 1) {
set.add(l.get(j));
}
}
System.out.println(set);
}
O/P :
[1, 4]

Removing duplicates from array without using Util classes

Please read the question before marking it as duplicate
I have written following code to remove duplicates from array without using Util classes but now I am stuck
public class RemoveDups{
public static void main(String[] args) {
int[] a = { 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 3, 1, 4, 52, 1, 45, };
int temp;
for (int i : a) {
for (int j = 0; j < a.length - 1; j++) {
if (a[j] > a[j + 1]) {
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
a = removeDups(a);
for (int i : a) {
System.out.println(i);
}
}
private static int[] removeDups(int[] a) {
int[] result = new int[a.length];
int j = 0;
for (int i : a) {
if (!isExist(result, i)) {
result[j++] = i;
}
}
return result;
}
private static boolean isExist(int[] result, int i) {
for (int j : result) {
if (j == i) {
return true;
}
}
return false;
}
}
and now the output is
1
2
3
4
5
6
45
52
0
0
0
0
0
0
0
0
0
0
Here my problem is
My code is not working in case of 0s
I am not able to understand how sorting an array can reduce time of execution
Is there any way to remove elements from array without using Util classes I know one way to remove convert array into list and then remove but for that also we need Util classes is there any way to implement by myself.
Since the numbers you deal with are limited to a small range you can remove duplicates by a simple "counting sort": mark the numbers you have found in a set-like data structure and then go over the data structure. An array of boolean works just fine, for less memory usage you could create a basic bitset or hash table. If n is the number of elements in the array and m is the size of the range, this algorithm will have O(n+m) complexity.
private static int[] removeDups(int[] a, int maxA) {
boolean[] present = new boolean[maxA+1];
int countUnique = 0;
for (int i : a) {
if (!present[i]) {
countUnique++;
present[i] = true;
}
}
int[] result = new int[countUnique];
int j = 0;
for (int i=0; i<present.length; i++) {
if (present[i]) result[j++] = i;
}
return result;
}
I am not able to understand how sorting an array can reduce time of execution
In a sorted array you can detect duplicates in a single scan, taking O(n) time. Since sorting is faster than checking each pair - O(n log n) compared to O(n²) time complexity - it would be faster to sort the array instead of using the naive algorithm.
As you are making the result array of the same length as array a
so even if you put only unique items in it, rest of the blank items will have the duplicate values in them which is 0 for int array.
Sorting will not help you much, as you code is searching the whole array again and again for the duplicates. You need to change your logic for it.
You can put some negative value like -1 for all the array items first in result array and then you can easily create a new result array say finalResult array from it by removing all the negative values from it, It will also help you to remove all the zeroes.
In java , arrays are of fixed length. Once created, their size can't be changed.
So you created an array of size18.
Then after you applied your logic , some elements got deleted. But array size won't change. So even though there are only 8 elements after the duplicate removal, the rest 10 elements will be auto-filled with 0 to keep the size at 18.
Solution ?
Store the new list in another array whose size is 8 ( or whatever, calculate how big the new array should be)
Keep a new variable to point to the end of the last valid element, in this case the index of 52. Mind you the array will still have the 0 values, you just won't use them.
I am not able to understand how sorting an array can reduce time of execution
What ? You sort an array if you need it to be sorted. Nothing else. Some algorithm may require the array to be sorted or may work better if the array is sorted. Depends on where you are using the array. In your case, the sorting will not help.
As for your final question , you can definitely implement your own duplicate removal by searching if an element exists more than once and then deleting all the duplicates.
My code is not working in case of 0
There were no zeroes to begin with in your array. But because its an int[], after the duplicates are removed the remaining of the indexes are filled with 0. That's why you can see a lot of zeroes in your array. To get rid of those 0s, you need to create another array with a lesser size(size should be equal to the no. of unique numbers you've in your array, excluding 0).
If you can sort your array(I see that its already sorted), then you could either bring all the zeroes to the front or push them to the last. Based on that, you can iterate the array and get the index from where the actual values start in the array. And, then you could use Arrays.copyOfRange(array, from, to) to create a copy of the array only with the required elements.
try this
package naveed.workingfiles;
public class RemoveDups {
public static void main(String[] args) {
int[] a = { 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 3, 1, 4, 52, 1, 45, };
removeDups(a);
}
private static void removeDups(int[] a) {
int[] result = new int[a.length];
int j = 0;
int count = 0;
for (int i : a) {
if (!isExist(result, i)) {
result[j++] = i;
count++;
}
}
System.out.println(count + "_____________");
for (int i=0;i<count;i++) {
System.out.println(result[i]);
}
// return result;
}
private static boolean isExist(int[] result, int i) {
for (int j : result) {
if (j == i) {
return true;
}
}
return false;
}
}
public class RemoveDups {
public static void main(String[] args) {
int[] a = { 1, 2, 0, 3, 1,0, 3, 6, 2};
removeDups(a);
}
private static void removeDups(int[] a) {
int[] result = new int[a.length];
int j = 0;
int count = 0;
boolean zeroExist = false;
for (int i : a) {
if(i==0 && !zeroExist){
result[j++] = i;
zeroExist = true;
count++;
}
if (!isExist(result, i)) {
result[j++] = i;
count++;
}
}
System.out.println(count + "_____________");
for (int i=0;i<count;i++) {
System.out.println(result[i]);
}
// return result;
}
private static boolean isExist(int[] result, int i) {
for (int j : result) {
if (j == i) {
return true;
}
}
return false;
}
}
// It works even Array contains 'Zero'
class Lab2 {
public static void main(String[] args) {
int[] a = { 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 3, 1, 4, 52, 1, 45 };
removeDups(a);
}
private static void removeDups(int[] a) {
int[] result = new int[a.length];
int j = 0;
int count = 0;
for (int i : a) {
if (!isExist(result, i)) {
result[j++] = i;
count++;
}
}
System.out.println(count + "_____________");
for (int i = 0; i < count; i++) {
System.out.println(result[i]);
}
}
private static boolean isExist(int[] result, int i) {
for (int j : result) {
if (j == i) {
return true;
}
}
return false;
}
}

Java, find intersection of two arrays

I have already read a few other stack overflow threads on this:
to find the intersection of two multisets in java
How do I get the intersection between two arrays as a new array?
public static int[] intersection (int [] x, int numELementsInX, int [] y, int numElementsInY) {
I am trying to examine two arrays as well as their number of elements (numElementsInX and numElementsInY), and return a new array which contains the common values of array x and y. Their intersection.
Example,if x is{1,3,5,7,9}and y is{9,3,9,4} then
intersection(x, 5, y, 4} should return {3, 9} or {9, 3}
I've read I need to use the LCS algorithm. Can anyone give me an example as to how to do this? Both the array and values in array are initialized and generated in another method, then passed into intersection.
Any help/clarification is appreciated.
EDIT CODE
for (int i=0; i<numElementsInX; i++){
for (int j=0; j<numElementsInY; j++){
if (x[j]==x[i]) { //how to push to new array?;
}
else{
}
}
}
The simplest solution would be to use sets, as long as you don't care that the elements in the result will have a different order, and that duplicates will be removed. The input arrays array1 and array2 are the Integer[] subarrays of the given int[] arrays corresponding to the number of elements that you intend to process:
Set<Integer> s1 = new HashSet<Integer>(Arrays.asList(array1));
Set<Integer> s2 = new HashSet<Integer>(Arrays.asList(array2));
s1.retainAll(s2);
Integer[] result = s1.toArray(new Integer[s1.size()]);
The above will return an Integer[], if needed it's simple to copy and convert its contents into an int[].
If you are fine with java-8, then the simplest solution I can think of is using streams and filter. An implementation is as follows:
public static int[] intersection(int[] a, int[] b) {
return Arrays.stream(a)
.distinct()
.filter(x -> Arrays.stream(b).anyMatch(y -> y == x))
.toArray();
}
General test
The answers provide several solutions, so I decided to figure out which one is the most effective.
Solutions
HashSet based by Óscar López
Stream based by Bilesh Ganguly
Foreach based by Ruchira Gayan Ranaweera
HashMap based by ikarayel
What we have
Two String arrays that contain 50% of the common elements.
Every element in each array is unique, so there are no duplicates
Testing code
public static void startTest(String name, Runnable test){
long start = System.nanoTime();
test.run();
long end = System.nanoTime();
System.out.println(name + ": " + (end - start) / 1000000. + " ms");
}
With use:
startTest("HashMap", () -> intersectHashMap(arr1, arr2));
startTest("HashSet", () -> intersectHashSet(arr1, arr2));
startTest("Foreach", () -> intersectForeach(arr1, arr2));
startTest("Stream ", () -> intersectStream(arr1, arr2));
Solutions code:
HashSet
public static String[] intersectHashSet(String[] arr1, String[] arr2){
HashSet<String> set = new HashSet<>(Arrays.asList(arr1));
set.retainAll(Arrays.asList(arr2));
return set.toArray(new String[0]);
}
Stream
public static String[] intersectStream(String[] arr1, String[] arr2){
return Arrays.stream(arr1)
.distinct()
.filter(x -> Arrays.asList(arr2).contains(x))
.toArray(String[]::new);
}
Foreach
public static String[] intersectForeach(String[] arr1, String[] arr2){
ArrayList<String> result = new ArrayList<>();
for(int i = 0; i < arr1.length; i++){
for(int r = 0; r < arr2.length; r++){
if(arr1[i].equals(arr2[r]))
result.add(arr1[i]);
}
}
return result.toArray(new String[0]);
}
HashMap
public static String[] intersectHashMap(String[] arr1, String[] arr2){
HashMap<String, Integer> map = new HashMap<>();
for (int i = 0; i < arr1.length; i++)
map.put(arr1[i], 1);
ArrayList<String> result = new ArrayList<>();
for(int i = 0; i < arr2.length; i++)
if(map.containsKey(arr2[i]))
result.add(arr2[i]);
return result.toArray(new String[0]);
}
Testing process
Let's see what happens if we give the methods an array of 20 elements:
HashMap: 0.105 ms
HashSet: 0.2185 ms
Foreach: 0.041 ms
Stream : 7.3629 ms
As we can see, the Foreach method does the best job. But the Stream method is almost 180 times slower.
Let's continue the test with 500 elements:
HashMap: 0.7147 ms
HashSet: 4.882 ms
Foreach: 7.8314 ms
Stream : 10.6681 ms
In this case, the results have changed dramatically. Now the most efficient is the HashMap method.
Next test with 10 000 elements:
HashMap: 4.875 ms
HashSet: 316.2864 ms
Foreach: 505.6547 ms
Stream : 292.6572 ms
The fastest is still the HashMap method. And the Foreach method has become quite slow.
Results
If there are < 50 elements, then it is best to use the Foreach method. He strongly breaks away in speed in this category.
In this case, the top of the best will look like this:
Foreach
HashMap
HashSet
Stream - Better not to use in this case
But if you need to process big data, then the best option would be use the HashMap based method.
So the top of the best look like this:
HashMap
HashSet
Stream
Foreach
With duplicate elements in array finding intersection.
int [] arr1 = {1,2,2,2,2,2,2,3,6,6,6,6,6,6,};
int [] arr2 = {7,5,3,6,6,2,2,3,6,6,6,6,6,6,6,6,};
Arrays.sort(arr1);
Arrays.sort(arr2);
ArrayList result = new ArrayList<>();
int i =0 ;
int j =0;
while(i< arr1.length && j<arr2.length){
if (arr1[i]>arr2[j]){
j++;
}else if (arr1[i]<arr2[j]){
i++;
}else {
result.add(arr1[i]);
i++;
j++;
}
}
System.out.println(result);
If you don't want to use other data structures such as a Set, then the basic idea is that you want to iterate through the elements of one of the arrays and for each value see if it appears in the other. How do you see whether it appears in the other array? Walk through the elements in the other array and for each one, see if its value is equal to the value you are looking for. I suspect that you will be best served by trying to work through this problem on your own beyond this point if your goal in taking the class is to learn to write Java well, but it you get stuck you might consider updating your question with the code that you have written so you can get more detailed feedback and pointers in the right direction.
Try this:
public static void main(String[] args) {
int[] arr1 = new int[]{1, 2, 3, 4, 5};
int[] arr2 = new int[]{3, 2, 5, 9, 11};
getIntersection(arr1, arr2);
}
public static Object[] getIntersection(int[] arr1, int[] arr2) {
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < arr1.length; i++) {
for (int j = 0; j < arr2.length; j++) {
if (arr1[i] == arr2[j]) {
list.add(arr1[i]);
}
}
}
return list.toArray();
}
You can find the intersection of two arrays with:
T[] result = Arrays.stream(a1)
.filter(new HashSet<>(Arrays.asList(a2))::contains)
.toArray(T[]::new);
where T should be substitutable by a reference type e.g. String, Integer, etc.
although the above may seem like it's creating a new set for each element, it's not the case at all. instead only one set instance is created.
The above code is equivalent to:
List<T> list = new ArrayList<>();
HashSet<T> container = new HashSet<>(Arrays.asList(a2));
for (T s : a1) {
if (container.contains(s)) list.add(s);
}
T[] result = list.toArray(new T[0]);
finding intersection includes duplicate using the hash map.
Output: 1 2 2 15 9 7 12
public static void main(String[] args) {
int[] arr1 = {1, 2, 2, 1, 5, 9, 15, 9, 7, 7, 12};
int[] arr2 = {1, 2, 2, 3, 4, 15, 9, 7, 12, 14};
printIntersect(arr1, arr2);
}
private static void printIntersect(int[] arr1, int[] arr2) {
Map<Integer, Integer> map = new HashMap<>();
//put first array to map
for (int i = 0; i < arr1.length; i++) {
if (!map.containsKey(arr1[i])) {
map.put(arr1[i], 1);
} else {
map.put(arr1[i], map.get(arr1[i]) + 1);
}
}
//check all value in array two
for (int i = 0; i < arr2.length; i++) {
//if exist and value>1 then decrement value
//if value is 1 remove from map
if (map.containsKey(arr2[i])) {
System.out.print(arr2[i] + " ");
if (map.get(arr2[i]) > 1) {
map.put(arr2[i], map.get(arr2[i]) - 1);
} else {
map.remove(arr2[i]);
}
}
}
}
if the arrays are sorted
int a1[]=new int[] {1,2,3,5,7,8};
int a2[]=new int [] {1,5,6,7,8,9};
// get the length of both the array
int n1=a1.length;
int n2=a2.length;
//create a new array to store the intersection
int a3[]=new int[n1];
//run the loop and find the intersection
int i=0,j=0,k=0;
while(i<n1&& j<n2) {
if(a1[i]<a2[j]) {
// a1 element at i are smaller than a2 element at j so increment i
i++;
}else if(a1[i]>a2[j]) {
// a2 element at i are smaller than a2 element at j so increment j
j++;
}else {
// intersection element store the value and increment i, j, k to find the next element
a3[k]=a1[i];
i++;
j++;
k++;
}
}
for(int l=0;l<a3.length;l++) {
System.out.println(a3[l]);
}
How to Find the Intersection of 3 unsorted arrays in Java:-
I have used the Core Java approach using for loops & using Arrays.copyOf to achieve this.
public class Intersection {
public void intersection3Arrays(int ar1[], int ar2[], int ar3[]) {
Arrays. sort(ar1);
Arrays. sort(ar2);
Arrays. sort(ar3);
int ar1Len = ar1.length;
int ar2Len = ar2.length;
int ar3Len = ar3.length;
int larArray = ar3Len > (ar1Len > ar2Len ? ar1Len : ar2Len) ? ar3Len : ((ar1Len > ar2Len) ? ar1Len : ar2Len);
System.out.println("The largest array is " +larArray);
int[] inputArray1 = Arrays.copyOf(ar1, larArray);
int[] inputArray2 = Arrays.copyOf(ar2, larArray);
int[] inputArray3 = Arrays.copyOf(ar3, larArray);
Integer[] inputArray11 = new Integer[inputArray1.length];
Integer[] inputArray22 = new Integer[inputArray2.length];
Integer[] inputArray33 = new Integer[inputArray3.length];
for (int i = 0; i < inputArray11.length; i++) {
if (inputArray11[i] == null){
inputArray1[i] = 0;
}
}
for (int i = 0; i < inputArray22.length; i++) {
if (inputArray22[i] == null){
inputArray1[i] = 0;
}
}
for (int i = 0; i < inputArray33.length; i++) {
if (inputArray33[i] == null){
inputArray1[i] = 0;
}
}
for (int i = 0; i < inputArray11.length; i++)
for (int j = 0; j < inputArray22.length; j++)
for (int k = 0; k < inputArray33.length; j++)
if (inputArray11[i] == inputArray22[j] && inputArray11[i] == inputArray33[k]) {
System.out.print(inputArray11[i]+" ");
}
}
public static void main(String[] args) {
Intersection3Arrays arrays = new Intersection3Arrays();
int ar1[] = { 1, 2, 5, 10, 20, 40, 80 };
int ar2[] = { 80, 100, 6, 2, 7, 20 };
int ar3[] = {3, 4, 15, 20, 30, 70, 80, 120};
arrays.intersection3Arrays(ar1, ar2, ar3);
}
}
If you ever wanted to implement this in python, this is one way that you can find intersection.
#find intersection
def find_intersec(list_a, list_b):
return set(list_a).intersection(list_b)
#since lists are kind of like arrays in python we use two lists
list_a = [ 4, 9, 1, 17, 11, 26, 28, 10,28, 26, 66, 91]
list_b = [9, 9, 74, 21, 45, 11, 63,10]
print(find_intersec(list_a, list_b))
I hope this example will simple one.pass two arrays and you will definitely get INTERSECTION of array without duplicate items.
private static int[] findInterserctorOfTwoArray(int[] array1, int[] array2) {
Map<Integer,Integer> map=new HashMap<>();
for (int element : array1) {
for (int element2 : array2) {
if(element==element2) {
map.put(element, element);
}
}
}
int[] newArray=new int[map.size()];
int con=0;
for(Map.Entry<Integer, Integer> lst:map.entrySet()) {
newArray[con]=lst.getValue();
con++;
}
return newArray;
}
optimised for sorted arrays using only one loop.
int a1[]=new int[] {1,2,3,5,7,8};
int a2[]=new int [] {1,5,6,7,8,9};
// sort both the array
Arrays.sort(a1);
Arrays.sort(a2);
// get the length of both the array
int n1=a1.length;
int n2=a2.length;
//create a new array to store the intersection
int a3[]=new int[n1];
//run the loop and find the intersection
int i=0,j=0,k=0;
while(i<n1&& j<n2) {
if(a1[i]<a2[j]) {
// a1 element at i are smaller than a2 element at j so increment i
i++;
}else if(a1[i]>a2[j]) {
// a2 element at i are smaller than a2 element at j so increment j
j++;
}else {
// intersection element store the value and increment i, j, k to find the next element
a3[k]=a1[i];
i++;
j++;
k++;
}
}
for(int l=0;l<a3.length;l++) {
System.out.println(a3[l]);
}
Primitive Iterator: 6 Times Faster than HashSet
Tested on sorted arrays of 10,000,000 random elements, values between 0 and 200,000,000. Tested on 10 processor i9 with 4GB heap space. Sort time for two arrays was 1.9 seconds.
results:
primitive() - 1.1 seconds
public static int[] primitive(int[] a1, int[] a2) {
List<Integer> list = new LinkedList<>();
OfInt it1 = Arrays.stream(a1).iterator();
OfInt it2 = Arrays.stream(a2).iterator();
int i1 = it1.next();
int i2 = it2.next();
do {
if (i1==i2) {
list.add(i1);
i1 = it1.next();
}
if (i1 < i2) i1 = it1.next();
if (i2 < i1) i2 = it2.next();
} while(it1.hasNext() && it2.hasNext());
if (i1==i2) list.add(i1);
return list.stream().mapToInt(Integer::intValue).toArray();
}
boxed() - 6.8 seconds
public static int[] boxed(int[] a1, int[] a2) {
return Arrays.stream(a1)
.filter(new HashSet<>(Arrays.stream(a2).boxed()
.collect(Collectors.toList()))::contains)
.toArray();
}

Is there a way to find common elements in multiple lists?

I have a list of integer arrays. I need to find the common elements between those. What I can think of is an extension of what is listed in Common elements in two lists
Example would be
[1,3,5],
[1,6,7,9,3],
[1,3,10,11]
should result in [1,3]
There are no duplicates in the arrays as well.
Is there a straight forward way to do this?
You can transform the lists to sets, and then use Set.retainAll method for intersection between the different sets.
Once you intersect all sets, you are left with the common elements, and you can transform the resulting set back to a list.
You can use Set's intersection method offered by Guava, Here is a little example :
public <T> Set<T> intersection(List<T>... list) {
Set<T> result = Sets.newHashSet(list[0]);
for (List<T> numbers : list) {
result = Sets.intersection(result, Sets.newHashSet(numbers));
}
return result;
}
Hope that could help you
We can use retainAll method of Collections. I initialised my commons arraylist with the first array list and called this for each remaining arraylists.
List<List<Integer>> lists = new ArrayList<List<Integer>>();
lists.add(new ArrayList<Integer>(Arrays.asList(1, 3, 5)));
lists.add(new ArrayList<Integer>(Arrays.asList(1, 6, 7, 9, 3)));
lists.add(new ArrayList<Integer>(Arrays.asList(1, 3, 10, 11)));
List<Integer> commons = new ArrayList<Integer>();
commons.addAll(lists.get(1));
for (ListIterator<List<Integer>> iter = lists.listIterator(1); iter.hasNext(); ) {
commons.retainAll(iter.next());
}
System.out.println(commons);
System.out.println(lists.get(1));
with Java 8
ArrayList retain = list1.stream()
.filter(list2::contains).filter(list3::contains).collect(toList())
If you are looking for a function that returns elements that exist in all lists,
then the straight forward & simple way is building a statistic { < member, occurences > }
The condition here is no duplicates among the same list,
private Set<Integer> getCommonElements(ArrayList<Integer[]> idList)
{
MapList<Integer,Short> stat = new MapList<Integer,Short>();
// Here we count how many times each value occur
for (int i = 0; i < idList.size(); i++)
{
for (int j = 0; j < idList.get(i).size; j++)
{
if (stat.containsKey(idList.get(i)[j]))
{
stat.set(idList.get(i)[j], stat.get(idList.get(i)[j])+1);
}
else
{
stat.add(idList.get(i)[j], 1);
}
}
}
// Here we only keep value that occured in all lists
for (int i = 0; i < stat.size(); i++)
{
if (stat.get(i) < idList.size())
{
stat.remove(i);
i--;
}
}
return stat.keySet();
}
public class ArrayListImpl{
public static void main(String s[]){
ArrayList<Integer> al1=new ArrayList<Integer>();
al1.add(21);al1.add(23);al1.add(25);al1.add(26);
ArrayList<Integer> al2=new ArrayList<Integer>();
al2.add(15);al2.add(16);al2.add(23);al2.add(25);
ArrayList Al3=new ArrayList<Integer>();
al3.addAll(al1);
System.out.println("Al3 Elements :"+al3);
al3.retainAll(al2); //Keeps common elements of (al1 & al2) & removes remaining elements
System.out.println("Common Elements Between Two Array List:"+al3);
}
}
If you are using JAVA 8 streams. Then using stream reduce operation we can achieve the same.
Considering your example: Let's say
a = [1,3,5], b = [1,6,7,9,3] and c = [1,3,10,11]
List<Integer> commonElements = Stream.of(a,b,c)
.reduce((s1,s2) -> {
s1.retainAll(s2);
return s1;
}).orElse(Collections.emptyList());
Keep in mind that after running this operation a will get modified with common values as well. So you will lose the actual value of a.
So elements of a and the result elements of commonElements will be essentially the same after running this operation.
Giving some another alternative code using retainAll capability of Set
public List getCommonItems(List... lists) {
Set<Integer> result = new HashSet<>(lists[0]);
for (List list : lists) {
result.retainAll(new HashSet<>(list));
}
return new ArrayList<>(result);;
}
Usage:
List list1 = [1, 2, 3]
List list2 = [3, 2, 1]
List list3 = [2, 5, 1]
List commonItems = getCommonItems(list1, list2, list3)
System.out.println("Common items: " + result);
Result:
commonItems: [1, 2]
public class commonvalue {
Public static void MyMethod(){
Set<integer> S1 = new set<integer>{1,3,5};
Set<integer> S2 = new set<integer>{1,6,7,9,3};
Set<integer> S3 = new set<integer>{1,3,10,11};
s2.retainall(s1);
s3.retainall(s2);
system.debug(s3);
}
}

Categories