I need to find the unique elements in two different arrays.
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] arr1 = new int[] { 1, 2, 3, 4, 5, 6 };
int[] arr2 = new int[] { 5, 6, 7, 8 };
boolean contains = false;
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]) {
contains = true;
break;
}
}
if(!contains){
list.add(arr1[i]);
}
else{
contains = false;
}
}
System.out.println(list);
}
But here I'm getting [1,2,3,4] as output. But the expected output is [1,2,3,4,7,8]. I'm not sure what I'm doing wrong here. And I need it in a traditional way. I don't want to use any inbuilt methods to acheive this.
Note: I feel it is not a duplicate because, the solution provided is not finding the unique elements on two arrays.
This solves your problem:
public static void main(String[] args) {
// Make the two lists
List<Integer> list1 = Arrays.asList(1, 2, 3, 4, 4, 5, 6);
List<Integer> list2 = Arrays.asList(5, 6, 7, 8);
// Prepare a union
Set<Integer> union = new HashSet<Integer>(list1);
union.addAll(list2);
// Prepare an intersection
Set<Integer> intersection = new HashSet<Integer>(list1);
intersection.retainAll(list2);
// Subtract the intersection from the union
union.removeAll(intersection);
// Print the result
for (Integer n : union) {
System.out.println(n);
}
}
Using HashSet, for educative purposes, which could be very fast if lists are big:
public static void main(final String[] args) {
final List<Integer> list1 = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6));
final Set<Integer> set1 = new HashSet<>(list1);
final List<Integer> list2 = new ArrayList<>(Arrays.asList(5, 6, 7, 8));
final Set<Integer> set2 = new HashSet<>(list2);
set1.retainAll(set2); // Keep union.
// Remove union to keep only unique items.
list1.removeAll(set1);
list2.removeAll(set1);
// Accumulate unique items.
list1.addAll(list2);
System.out.println(new HashSet<>(list1));
// [1,2,3,4,7,8]
}
Actually, there is a more simple solution using Java TreeSet.java TreeSet doesn't contain duplicate elements. Therefore, all you have to do is create a TreeSet and adding all elements to it. It also keeps the natural (sorted) Order.
public static void main(String[] args) {
int[] arr1 = new int[] { 1, 2, 3, 4, 5, 6 };
int[] arr2 = new int[] { 5, 6, 7, 8 };
TreeSet<Integer> set = new TreeSet<>();
for (int i:arr1) {
set.add(i);
}
for (int i:arr2) {
set.add(i);
}
System.out.println(set);
}
output: [1, 2, 3, 4, 5, 6, 7, 8]
If you are using java 8 i would suggest this solution :
public static void main(String[] args) {
int[] arr1 = new int[]{1, 2, 3, 4, 5, 6};
int[] arr2 = new int[]{5, 6, 7, 8};
List<Integer> list = new ArrayList<>();//create a list or Integers
//add the values of the two arrays in this list
list.addAll(Arrays.stream(arr1).boxed().collect(Collectors.toList()));
list.addAll(Arrays.stream(arr2).boxed().collect(Collectors.toList()));
//we need a set to check if the element is duplicate or not
Set<Integer> set = new HashSet();
List<Integer> result = new ArrayList<>(list);
//loop throw your list, and check if you can add this element to the set
// or not, if not this mean it is duplicate you have to remove it from your list
list.stream().filter((i) -> (!set.add(i))).forEachOrdered((i) -> {
result.removeAll(Collections.singleton(i));
});
System.out.println(result);
}
Output
[1, 2, 3, 4, 7, 8]
To solve this problem, i based to this posts : Identify duplicates in a List
And here another streaming (Java 8) solution. Using streams one should avoid modifying stream outside variables.
The idea here is to union the lists and then to count the occurance of each item. All items with count 1 are only in one list. Those are collected to the result list.
//using here Integer instead of atomic int, simplifies the union.
Integer[] arr1 = new Integer[]{1, 2, 3, 4, 5, 6};
Integer[] arr2 = new Integer[]{5, 6, 7, 8};
List<Integer> list = new ArrayList<>();
list.addAll(new HashSet<>(Arrays.asList(arr1)));
list.addAll(new HashSet<>(Arrays.asList(arr2)));
System.out.println(
list.stream()
.collect(groupingBy(identity(), counting()))
.entrySet().stream()
.filter(i -> i.getValue() == 1)
.map(i -> i.getKey())
.collect(toList())
);
EDIT:
Changed this answer to adress multiples within one list problem.
You have to add a second for-loop to check if elements of arr2 are in arr1 cause you are only checking if elements of arr1 are in arr2
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] arr1 = new int[] { 1, 2, 3, 4, 5, 6 };
int[] arr2 = new int[] { 5, 6, 7, 8 };
boolean contains = false;
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]) {
contains = true;
break;
}
}
if(!contains){
list.add(arr1[i]);
}
else{
contains = false;
}
}
for (int i = 0; i < arr2.length; i++) {
for (int j = 0; j < arr1.length; j++) {
if (arr1[i] == arr2[j]) {
contains = true;
break;
}
}
if(!contains){
list.add(arr2[i]);
}
else{
contains = false;
}
}
System.out.println(list);
}
A more optimized way would be using list iterators.
int[] arr1 = new int[] { 1, 2, 3, 4, 5, 6 };
int[] arr2 = new int[] { 5, 6, 7, 8 };
List<Integer> list1 = IntStream.of(arr1).boxed().collect(Collectors.toList());
List<Integer> list2 = IntStream.of(arr2).boxed().collect(Collectors.toList());
Iterator list1Iter = list1.iterator();
boolean contains = false;
while(list1Iter.hasNext()) {
int val1 = (int)list1Iter.next();
Iterator list2Iter = list2.iterator();
while(list2Iter.hasNext()) {
int val2 = (int)list2Iter.next();
if( val1 == val2) {
// remove duplicate
list1Iter.remove();
list2Iter.remove();
}
}
}
list1.addAll(list2);
for( Object val : list1) {
System.out.println(val);
}
If you are using Java 8, you can do the following :
List resultList = list1.stream().filter(nbr -> !list2.contains(nbr)).collect(Collectors.toList());
resultList.addAll(list2.stream().filter(nbr -> !list1.contains(nbr)).collect(Collectors.toList()));
import java.util.Scanner;
import java.io.*;
public class CandidateCode{
static int count =0;
public static void main(String args[])
{
int n,n1;
Scanner sc=new Scanner(System.in);
System.out.println("Enter no. of elements for first array");
n=sc.nextInt();
int arr[]=new int[n];
System.out.println("Enter the elements of first array");
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
System.out.println("Enter no. of elements for second array");
n1=sc.nextInt();
int arr1[]=new int[n1];
System.out.println("Enter the elements of second array");
for(int i=0;i<n1;i++)
{
arr1[i]=sc.nextInt();
}
unique_ele(arr,arr1);
unique_ele(arr1,arr);
System.out.println("The number of unique elements are");
System.out.println(count);
}
public static int unique_ele(int arr2[],int arr3[])
{
boolean contains = false;
for(int i=0;i<arr2.length;i++)
{
for(int j=0;j<arr3.length;j++)
{
if (arr2[i] == arr3[j]) {
contains = true;
break;
}
}
if(!contains){
count++;
}
else{
contains = false;
}
}
return count;
}
}
public static ArrayList<Integer> findUniqueAmongLists(ArrayList<Integer> a, ArrayList<Integer> b){
ArrayList<Integer> uniqueArr = new ArrayList<>();
ArrayList<Integer> duplicateArr = new ArrayList<>();
for(int i=0; i< a.size(); i++){
if(!duplicateArr.contains(a.get(i))){
uniqueArr.add(a.get(i));
duplicateArr.add(a.get(i));
}
else{
uniqueArr.remove(a.get(i));
}
}
for(int j=0; j< b.size(); j++){
if(!duplicateArr.contains(b.get(j))){
uniqueArr.add(b.get(j));
duplicateArr.add(b.get(j));
}
else{
uniqueArr.remove(b.get(j));
}
}
return uniqueArr;
}
int[] arr1 = new int[] { 1, 2, 3, 4, 5, 6 };
int[] arr2 = new int[] { 5, 6, 7, 8 };
boolean contains = false;
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]) {
contains = true;
break;
}
}
if (!contains) {
list.add(arr1[i]);
} else {
contains = false;
}
}
for (int j = 0; j < arr2.length; j++) {
for (int k = 0; k < arr1.length; k++) {
if (arr2[j] == arr1[k]) {
contains = true;
break;
}
}
if (!contains) {
list.add(arr2[j]);
} else {
contains = false;
}
}
System.out.println(list);
}
public class UniqueElementFrom2array {
public static void main(String[] args)
{
int[] a= {1,2,3,4,5,6,7};
int[] b= {1,2,3,8,9,4,10,11,12};
int[] c=new int[a.length+b.length];
int len1=a.length;
int len2=b.length;
System.arraycopy(a, 0, c, 0, len1);
System.arraycopy(b, 0, c, len1,len2);
Arrays.sort(c);
System.out.println(Arrays.toString(c));
Set s=new HashSet();
for(int i=0;i<c.length;i++)
{
if(!s.contains(c[i]))
{
s.add(c[i]);
System.out.print(c[i] + " ");
}
}
}
}
A complete code using TreeSet in java.
import java.util.*;
import java.util.Scanner;
public class Main
{
public static void uniqElements(int arr1[], int arr2[],int n){
TreeSet<Integer> set1 = new TreeSet<>();
TreeSet<Integer> set2 = new TreeSet<>();
TreeSet<Integer> set3 = new TreeSet<>();
TreeSet<Integer> set4 = new TreeSet<>();
for (int i:arr1) {
set1.add(i);
set3.add(i);
}
for (int i:arr2) {
set2.add(i);
set4.add(i);
}
set3.addAll(set4);
set1.retainAll(set2);
set3.removeAll(set1);
System.out.println(set3);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr1 = new int[n];
int[] arr2 = new int[n];
for(int i =0;i<n;i++){
arr1[i]=sc.nextInt();
}
for(int i =0;i<n;i++){
arr2[i]=sc.nextInt();
}
uniqElements(arr1,arr2,n);
}
}
public static void main(String[] args) {
int[] arr1 = new int[] { 1, 2, 3, 4, 5, 6 };
int[] arr2 = new int[] { 5, 6, 7, 8 };
System.out.println(Stream.concat(Arrays.stream(arr1).boxed(), Arrays.stream(arr2).boxed()).distinct().collect(Collectors.toList()));
}
Merge all array.
Remove elements at merged array that contains second array value.
import java.util.TreeSet;
public class ArrayUnique {
public static void getUniqueElements(int arr1[], int arr2[]){
// Merge all array with treeset
// it will remove duplicate element
TreeSet<Integer> all= new TreeSet<>();
TreeSet<Integer> secondArray= new TreeSet<>();
for(int i:arr1){
all.add(i);
}
for(int i:arr2){
all.add(i);
secondArray.add(i);
}
//Delete element that contains secondArray value
all.removeAll(secondArray);
//print to console
System.out.println(all);
}
public static void main(String[] args) {
int[] arr1 = {1,2,3,4};
int[] arr2 = {1,3,5,10,16};
getUniqueElements(arr1,arr2);
//Result: [2,4]
}
}
Try this:
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] arr1 = new int[] { 1, 2, 3, 4, 5, 6 };
int[] arr2 = new int[] { 5, 6, 7, 8 };
List<Integer> list = Stream.concat(Arrays.stream(arr1).boxed(), Arrays.stream(arr2).boxed()).distinct().collect(Collectors.toList());
System.out.println(list);
}
hope this will resolve issue.
This will give you the unique values from two arrays;
public static String[] uniqueArr(String[] names1, String[]
names2)
{
Set<String> set = new HashSet<>(new
LinkedList<>(Stream.of(names1,
names2).flatMap(Stream::of).collect(Collectors.toList())));
String[] arr = new String[set.size()];
set.toArray(arr);
return arr;
}
Related
I'm trying to compare two int arrays, where array 1 is the standard (1...n) and array 2 is random numbers within the range of (1...n). The numbers that are missing from array 2 need to be printed out. So far, I've managed the following, but I can't seem to figure out how to use the boolean array to my benefit.
import java.util.Scanner;
public class findLeaves {
private boolean[] id;
private String[] surface;
private int[] head;
public boolean[] inputId() {
System.out.println("Input number of IDs");
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
id = new boolean[n];
return this.id;
}
public int[] inputHead() {
System.out.println("Input each head value.");
head = new int[id.length];
for (int i = 0; i < id.length; i++){
Scanner scan = new Scanner(System.in);
head[i] = scan.nextInt();
}
return this.head;
}
public boolean Leaves() {
for (int i = 0; i < head.length; i++);
for (int j = 0; j < id.length; j++) {
if (!id[j]) System.out.print(j + ",");
}
return true;
}
public static void main(String[] args) {
findLeaves x = new findLeaves();
x.inputId();
x.inputHead();
x.Leaves();
}
}
Currently, Leaves() is just printing out:
0,1,2,3,4,5,6,7,8,
Does anyone know of a way that this can be accomplished? I'm relatively new to Java, and haven't been able to find anything googling or here that solves my problem. Thanks in advance!
Edit:
When I update Leaves to be:
public boolean Leaves() {
for (int i = 0; i < head.length; i++) id[head[i]] = true;
for (int j = 0; j < id.length; j++) {
if (!id[j]) System.out.print(j + ",");
}
return true;
}
I get an error of:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9
at findLeaves.Leaves(findLeaves.java:28)
at findLeaves.main(findLeaves.java:39)
First of all, you have termination point here for (int i = 0; i < head.length; i++);, so you'll not even run this loop.
This is a shorter way to find missing value from original array in random:
import java.util.Arrays;
public class Main {
private int[] original = new int[] {1, 3, 5, 7, 9}; // original array
private int[] random = new int[] {1, 2, 3, 4, 5, 6}; // missing value from original array will be printed
public static void main(String[] args) {
Main m = new Main();
Arrays.sort(m.original); // sort is required for binarySearch()
for (int i : m.random) {
if (Arrays.binarySearch(m.original, i) < 0)
System.out.println(i);
}
}
}
With Java 8+:
import java.util.stream.IntStream;
public class Main {
private int[] original = new int[] {1, 3, 5, 7, 9};
private int[] random = new int[] {1, 2, 3, 4, 5, 6};
public static void main(String[] args) {
Main m = new Main();
for (int i : m.random) {
if (IntStream.of(m.original).noneMatch(value -> value == i))
System.out.println(i);
}
}
}
Without any libraries:
public class Main {
private int[] original = new int[] {1, 3, 5, 7, 9};
private int[] random = new int[] {1, 2, 3, 4, 5, 6};
public static void main(String[] args) {
Main m = new Main();
for (int i : m.random) {
if (!contains(m.original, i))
System.out.println(i);
}
}
public static boolean contains(int[] array, int value) {
for (int i : array)
if (i == value)
return true;
return false;
}
}
Output:
2
4
6
change this code
public boolean Leaves() {
for (int i = 0; i < head.length; i++) id[head[i] -1] = true;
for (int j = 0; j < id.length; j++) {
if (!id[j]) System.out.print((j +1)+ ",");
}
return true;
}
How can i fill Array from Array. Example:
Array Main = {1,2,3,4,5,6,7,8,9,10}
and i want to fill Array 1 and Array 2 like :
Array 1 = {1,3,5,7,9}
Array 2 = {2,4,6,8,10}
I guess i need to use a for-loop on Array Main and push the int values to Array 1 and Array 2. but i don't know how to do this in Java Anyone can help me
my code:
import java.util.ArrayList;
import java.util.List;
public class myArray {
/**
* #param args
*/
public static void main(String[] args) {
// Your Prepared data.
List<int[]> values = new ArrayList<int[]>(2);
int[] element1 = new int[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
values.add(element1);
// Add the result to ArrayList.
List<Integer> temp = new ArrayList<Integer>();
List<Integer> temp1 = new ArrayList<Integer>();
for(int j=0;j<values.size(); j++) {
for (int i = 0; i < values.get(0).length; i++) {
if (values.get(0)[i] < 2 ){
temp.add(values.get(0)[i]);
} else {
temp1.add(values.get(0)[i]);
}
}
}
System.out.println(temp);
System.out.println(temp1);
}
}
This is a better solution:
import java.util.ArrayList;
import java.util.List;
public class myArray {
/**
* #param args
*/
public static void main(String[] args) {
int[] values = new int[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// Add the result to ArrayList.
List<Integer> temp = new ArrayList<Integer>();
List<Integer> temp1 = new ArrayList<Integer>();
for(int i=0;i<values.length; i++) {
if (values[i] % 2 == 0){
temp.add(values[i]);
} else {
temp1.add(values[i]);
}
}
System.out.println(temp);
System.out.println(temp1);
}
}
I Think This is a Better Solution
import java.awt.List;
import java.util.ArrayList;
public class myArray {
public static void main(String[] args) {
// Your Prepared data.
ArrayList<int[]> values = new ArrayList<int[]>(2);
int[] element1 = new int[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
values.add(element1);
// Add the result to ArrayList.
ArrayList<Integer> temp = new ArrayList<Integer>();
ArrayList<Integer> temp1 = new ArrayList<Integer>();
for(int j=0;j<values.size(); j++) {
for (int i = 0; i < values.get(0).length; i++) {
if (values.get(0)[i] % 2 == 0 ){
temp.add(values.get(0)[i]);
} else {
temp1.add(values.get(0)[i]);
}
}
}
System.out.println(temp);
System.out.println(temp1);
}
}
I have to create two arrays with ten random values between 0 and 20. With those arrays create two more arrays:
arrayReps contains the numbers that match between the two arrays. For example with arrays of 3 values: [3, 2, 4], [4, 5, 2] then arrayReps = [2, 4] (order doesnt matter)
arrayUniq contains the unique values of each of the original arrays. From the previous example, arrayUniq = [3, 5].
I already created the two random arrays, but i am having trouble on the arrayReps and arrayUniq, can you help me out? My Code:
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class P03ArraysExtractUniqsAndReps {
public static void main(String[] args){
Scanner scan1 = new Scanner(System.in);
Random number = new Random();
int a = 0;
int[] randomArray1;
randomArray1 = new int[10];
int[] randomArray2;
randomArray2 = new int[10];
int[] arrayUniq;
int[] arrayReps;
//int[] arrayUniq = new int[10];
for(int i = 0; i < 10; i++){
randomArray1[i] = number.nextInt(20);
}
for(int i = 0; i < 10; i++){
randomArray2[i] = number.nextInt(20);
}
System.out.println(Arrays.toString(randomArray1));
System.out.println(Arrays.toString(randomArray2));
}
}
Compare each element of a1 with the elements of a2. If the match is found, store that element in arrayReps, else store the element in arrayUniq
Something like this:
for(int i=0;i<a1.length;i++)
{
for(int j=0;j<a2.length;j++)
{
if(a1[i]==a2[j])
//add the element in arrayReps
else
//add the element in arrayUniq
}
}
you can also use bucket sort which is the most efficient and the conditions of bucketSort are met: the max and min values are known.
int[] bucketSortArray = new int[20];
for(int i=0;i<10;i++){
bucketSortArray[randomArray1[i]]++;
bucketSortArray[randomArray2[i]]++;
}
System.out.println(Arrays.toString(bucketSortArray));
if bucketSortArray[15]==1, than 15 is unique.
if bucketSortArray[20]>1, than 20 is in Reps.
if it's 0, ignore.
Here's one way of doing it.
Compare the first list of numbers against the second list, then whatever is left in the second list must also be unique.
import java.util.ArrayList;
public class UniqueArray {
public static void main(String[] args)
{
int[] list1 = {0,4,6,2,8,9};
int[] list2 = {0,4,3,2,7,5};
ArrayList<Integer> unique = new ArrayList<Integer>();
ArrayList<Integer> dups = new ArrayList<Integer>();
boolean isUnique = false;
int num;
for(int i = 0; i < list1.length; i++)
{
num = list1[i];
for(int j = 0; j < list1.length; j++)
{
isUnique = true;
if(list1[i] == list2[j])
{
isUnique = false;
list2[j] = -1; // mark as having already been found
break;
}
}
if(isUnique)
{
unique.add(num);
}
else
{
dups.add(num);
}
}
for(int i=0; i< list2.length; i++)
{
if(list2[i]!= -1)
{
unique.add(list2[i]);
}
}
System.out.println("Unique:");
System.out.println(unique);
System.out.println("Dups:");
System.out.println(dups);
}
}
Here is the solution, grab one or both as you wish:
Duplicates O(n+m)
public static int[] getDuplicates(int[] array1, int[] array2) {
HashSet<Integer> duplicates = new HashSet<>();
HashSet<Integer> uniques = new HashSet<>();
for(int i : array1) {
if (!uniques.add(i)) {
duplicates.add(i);
}
}
for(int j : array2) {
if (!uniques.add(j)) {
duplicates.add(j);
}
}
return duplicates.stream().mapToInt(i->i).toArray();
}
Uniques O(n+m)
public static int[] getUniques(int[] array1, int[] array2) {
HashSet<Integer> uniques = new HashSet<>();
for(int i : array1) {
uniques.add(i);
}
for (int j : array2) {
uniques.add(j);
}
return uniques.stream().mapToInt(i->i).toArray();
}
All at once O(n+m)
In case you don't want to use two methods, just put the body of getDuplicates(..) method (it calculates both of duplicates and uniques) to your main(..) one.
public static void main(String[] args) {
int[] array1 = {1,2,5,6,8,9,1};
int[] array2 = {3,4,5,6,0};
HashSet<Integer> setDuplicates = new HashSet<>();
HashSet<Integer> setUniques = new HashSet<>();
for (int i : array1) {
if (!setUniques.add(i)) {
setDuplicates.add(i);
}
}
for (int j : array2) {
if (!setUniques.add(j)) {
setDuplicates.add(j);
}
}
int[] duplicates = setDuplicates.stream().mapToInt(i->i).toArray();
int[] uniques = setUniques.stream().mapToInt(i->i).toArray();
System.out.println(Arrays.toString(duplicates));
System.out.println(Arrays.toString(uniques));
}
All methods will give you the same output:
Duplicates: [1, 5, 6]
Uniques: [0, 1, 2, 3, 4, 5, 6, 8, 9]
I have an ArrayList of arrays and i want to change the value of 5
List<int[]> list = new ArrayList<int[]>();
int[] arr1 = {2,4,6};
int[] arr2 = {1,3,5};
list.add(arr1);
list.add(arr2);
for (int[] ss : list)
{
for(int sd : ss)
{
//System.out.println(sd);
if(sd == 5)
{
System.out.println("Yes");
//change 5 to 12
//list.set(list.indexOf(5), 12);
}
}
System.out.println(Arrays.toString(ss));
}
[2, 4, 6] [1, 3, 5]
I want to change 5 to 12
Change your code to :
public static void main(String args[]) {
List<int[]> list = new ArrayList<int[]>();
int[] arr1 = { 2, 4, 6 };
int[] arr2 = { 1, 3, 5 };
list.add(arr1);
list.add(arr2);
for (int[] ss : list) {
for (int i = 0; i < ss.length; i++) {
// System.out.println(sd);
if (ss[i] == 5) { // if current value is 5
System.out.println("Yes");
ss[i] = 12; // set it to 12
}
}
System.out.println(Arrays.toString(ss));
}
}
I need to efficiently sort through 4 arrays and return 2 results.
The first result will contain all numbers that exist in the arrays.
The second result will contain, all occurrences of each number in the result
int[] a = [1,2,3,4,5];
int[] b = [1,2,3,4,5,6];
int[] c = [1,3,7];
int[] d = [2,3,4,8,9,10];
int[] result1 = [1,2,3,4,5,6,7,8,9,10];
int[] result2 = [1,1,1,2,2,2,3,3,3,3,4,4,4,5,5,6,7,8,9,10];
Step 1: Combine all the arrays
Step 2: Sort arrays using Arrays.sort(array);
Step 3: Remove the duplicates.
int[] a = {1,2,3,4,5};
int[] b = {1,2,3,4,5,6};
int[] c = {1,3,7};
int[] d = {2,3,4,8,9,10};
int[] resultArray1 = new int[a.length+b.length+c.length+d.length];
int arrayIndex = 0;
for (int i=0; i< a.length ; i++, arrayIndex++ )
{
resultArray1[arrayIndex] = a[i];
}
for (int i=0; i< b.length ; i++, arrayIndex++ )
{
resultArray1[arrayIndex] = b[i];
}
for (int i=0; i< c.length ; i++, arrayIndex++ )
{
resultArray1[arrayIndex] = c[i];
}
for (int i=0; i< d.length ; i++, arrayIndex++ )
{
resultArray1[arrayIndex] = d[i];
}
// Sorting Arrays
System.out.println("Array before Sort"+Arrays.toString(resultArray1));
Arrays.sort(resultArray1);
System.out.println("Array after Sort"+Arrays.toString(resultArray1));
// Removing duplicates
Set<String> set = new HashSet<String>();
for (int i = 0; i < resultArray1.length; i++) {
set.add(""+resultArray1[i]); // To convert to string
}
String[] uniqueStringArray = set.toArray(new String[set.size()]); ;
int [] uniqueIntArray = new int [uniqueStringArray.length];
// Converting string array to int array
for(int i=0;i<uniqueStringArray.length;i++)
{
uniqueIntArray[i]= Integer.parseInt(uniqueStringArray[i]);
}
Arrays.sort(uniqueIntArray);
System.out.println("Unique Array after Sort"+Arrays.toString(uniqueIntArray));
Output:
Array before Sort[1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 6, 1, 3, 7, 2, 3, 4, 8, 9, 10]
Array after Sort[1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 5, 5, 6, 7, 8, 9, 10]
Unique Array after Sort[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Combine all 4 arrays in 1 array
sort Array
Remove Dublicate from array
import java.util.Vector;
class sort
{
public static void main(String args[])
{
int[] a = {1,2,3,4,5};
int[] b = {1,2,3,4,5,6};
int[] c = {1,3,7};
int[] d = {2,3,4,8,9,10};
int[] s = combine(a,b,c,d); //concate array using concate function
int[] sort = sort(s);//sort array using sort function
//SORTING ARRAY
System.out.print("ARRAY AFTER SORT ");
System.out.print("[");
for(int i=0;i<sort.length;i++)
{
if(i==sort.length-1)
System.out.print(sort[i]);
else
System.out.print(sort[i]+",");
}
System.out.println("]\n");
//REMOVE DUPLICATE
int[] removedups=removedups(sort);// Remove Duplicate item from array using removedups
System.out.print("ARRAY REMOVE DUPLICATE ");
System.out.print("[");
for(int i=0;i<removedups.length;i++)
{
if(i==removedups.length-1)
System.out.print(removedups[i]);
else
System.out.print(removedups[i]+",");
}
System.out.print("]");
}
public static int[] combine(int a[],int b[],int c[],int d[])//combine array
{
int sort []=new int[a.length+b.length+c.length+d.length];
int j=0;
for(int i=0;i<a.length;i++)
{
sort[j]=a[i];
j++;
}
for(int i=0;i<b.length;i++)
{
sort[j]=b[i];
j++;
}
for(int i=0;i<c.length;i++)
{
sort[j]=c[i];
j++;
}
for(int i=0;i<d.length;i++)
{
sort[j]=d[i];
j++;
}
return sort;
}
public static int[] sort(int s[]) // sort array
{
int temp;
for(int i=0;i<s.length;i++)
{
for(int j=i+1;j<s.length-1;j++)
{
if(s[i]>s[j])
{
temp=s[i];
s[i]=s[j];
s[j]=temp;
}
}
}
return s;
}
public static int[] removedups(int s[])//remove dups from sorted array
{
Vector array=new Vector();
int l=0;
for(int i=0;i<s.length-1;i++)
{
if(s[i]!=s[i+1])
{
array.add(s[i]);
l++;
}
}
int[] temp=new int[array.size()];
for(int j=0;j<temp.length;j++)
{
temp[j]=Integer.parseInt(""+array.get(j));
}
return temp ;
}
}//end the program
Simpliest way is use sort and HashSet
public class SortClass {
public static void main(String[] args) {
//Define imput data
int[] a = {1, 2, 3, 4, 5};
int[] b = {1, 2, 3, 4, 5, 6};
int[] c = {1, 3, 7};
int[] d = {2, 3, 4, 8, 9, 10};
//Actual algorithm
List<Integer> all = new ArrayList<Integer>();
all.addAll(toList(a));
all.addAll(toList(b));
all.addAll(toList(c));
all.addAll(toList(d));
Collections.sort(all);
TreeSet<Integer> set = new TreeSet<Integer>();
set.addAll(all);
int[] sorted = toIntArray(all);
int[] sortedUniq = toIntArray(set);
//Output result
System.out.println("Sorted: " + Arrays.toString(sorted));
System.out.println("Sorted uniq: " + Arrays.toString(sortedUniq));
}
// We need pair of method to convert between `int[]` and `Collection<Integer>`
private static int[] toIntArray(Collection<Integer> all) {
int[] ints = new int[all.size()];
int i =0;
for (int val : all) {
ints[i] = val;
i++;
}
return ints;
}
private static Collection<? extends Integer> toList(int[] ints) {
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i : ints) {
list.add(i);
}
return list;
}
}
But if performance is critical we can utilize the fact that input arrays already sorted and us merge sort:
public class SortClass {
public static void main(String[] args) {
int[] a = {1, 2, 3, 4, 5};
int[] b = {1, 2, 3, 4, 5, 6};
int[] c = {1, 3, 7};
int[] d = {2, 3, 4, 8, 9, 10};
//Prepare data structure
List<ArrayTail> all = new ArrayList<ArrayTail>();
add(all, new ArrayTail(a));
add(all, new ArrayTail(b));
add(all, new ArrayTail(c));
add(all, new ArrayTail(d));
int[] sorted = sort(all);
int[] sortedUniq = getUniq(sorted);
System.out.println("Sorted: " + Arrays.toString(sorted));
System.out.println("Sorted uniq: " + Arrays.toString(sortedUniq));
}
private static int[] getUniq(int[] sorted) {
Collection<Integer> result = new ArrayList<Integer>();
int current = sorted[0];
result.add(current);
for (int i : sorted){
if(i != current){
current = i;
result.add(i);
}
}
return toIntArray(result);
}
private static int[] sort(List<ArrayTail> all) {
int totalLength = 0;
for (ArrayTail tail : all){
totalLength+=tail.size();
}
int[] result = new int[totalLength];
int pos = 0;
while(!all.isEmpty()){
//Take smallest value from smallest array
ArrayTail smallest = all.get(0);
result[pos] = smallest.take();
pos++;
// remove array if no more elements in it
if(smallest.size() ==0){
all.remove(0);
} else {
// ensure that first element steel smallest
sortFirstElement(all);
}
}
return result;
}
// This is actually on step of bubble sort, but it
// works because all other list except may be first already sorted
private static void sortFirstElement(List<ArrayTail> all) {
for (int i = 0; i < all.size()-1; i++) {
if(all.get(i).get() > all.get(i+1).get()){
Collections.swap(all, i, i + 1);
} else {
break;
}
}
}
private static void add(List<ArrayTail> all, ArrayTail arrayTail) {
// all sorted here
all.add(0, arrayTail);
sortFirstElement(all);
// all sorted here, again
}
private static int[] toIntArray(Collection<Integer> all) {
int[] ints = new int[all.size()];
int i =0;
for (int val : all) {
ints[i] = val;
i++;
}
return ints;
}
// Simpl data structure representing tail of array
private static class ArrayTail {
private final int[] arr;
private int pos;
public ArrayTail(int[] arr) {
this.arr = arr;
this.pos = 0;
}
public int size() {
return arr.length - pos;
}
public int take() {
return arr[pos++];
}
public int get() {
return arr[pos];
}
}
}
As you can see code become more complex but possible work faster.
PS: fun thing is that bubble sort can be used in real life :)
TRY Using merge sort algorithm. wikipedia page here
For result 1, dump the contents of your 4 lists into a TreeSet. This will sort and remove duplicates from the set at the same time. Then get the array of the set.
For result 2, use array copy to copy all the numbers into 1 array and then sort that array using Arrays.sort(...). There is no need to try to merge them on your own due to the nature of the sorting algorithm which is O(nlog(n)). Quadrupling the size results in a multiplying factor of only 2.