Array Programs
Array Programs
Output :
import java.util.HashSet;
---------------------------------
import java.util.Arrays;
firstLargest = input[0];
secondLargest = input[1];
}
else
{
//If second element is greater than first element
firstLargest = input[1];
secondLargest = input[0];
}
secondLargest = firstLargest;
firstLargest = input[i];
}
else if (input[i] < firstLargest && input[i] > secondLargest)
{
//If element at 'i' is smaller than 'firstLargest' and greater than
'secondLargest'
secondLargest = input[i];
}
}
System.out.println(Arrays.toString(input));
System.out.println(secondLargest);
}
Input Array :
[47498, 14526, 74562, 42681, 75283, 45796]
Second Largest Element :
74562
Step 1:
Iterate the given array
Step 2 (first if condition arr[i] > largest):
If current array value is greater than largest value then
Move the largest value to secondLargest and make
current value as largest
Step 3 (second if condition arr[i] > secondLargest )
If the current value is smaller than largest and greater than secondLargest then
the current value becomes secondLargest
package com.candidjava;
int arr[] = { 14, 46, 47, 86, 92, 52, 48, 36, 66, 85 };
int largest = arr[0];
int secondLargest = arr[0];
}
}
}
}
Output
Largest Number: 99
Second Largest Number: 78
To find the second largest element of the given array, first of all, sort the
array.
Sorting an array
Compare the first two elements of the array
If the first element is greater than the second swap them.
Then, compare 2nd and 3rd elements if the second element is greater than the 3rd
swap them.
Repeat this till the end of the array.
After sorting an array print the second element from the end of the array.
Program
Live Demo
if(array[i]>array[j]){
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
System.out.println("Third second largest number is:: "+array[size-2]);
}
}
Output
Third second largest number is:: 63
Another solution
You can also sort the elements of the given array using the sort method of the
java.util.Arrays class then, print the second element from the end of the array.
import java.util.Arrays;
public class LargestNumberSample {
public static void main(String args[]){
int array[] = {10, 20, 25, 63, 96, 57};
int size = array.length;
Arrays.sort(array);
System.out.println("sorted Array ::"+Arrays.toString(array));
int res = array[size-2];
System.out.println("2nd largest element is ::"+res);
}
}
Output
sorted Array ::[10, 20, 25, 57, 63, 96]
2nd largest element is ::63
---------------------------------
import java.util.Arrays;
if(arrayOne.length == arrayTwo.length)
{
for (int i = 0; i < arrayOne.length; i++)
{
if(arrayOne[i] != arrayTwo[i])
{
equalOrNot = false;
}
}
}
else
{
equalOrNot = false;
}
if (equalOrNot)
{
System.out.println("Two Arrays Are Equal");
}
else
{
System.out.println("Two Arrays Are Not equal");
}
}
}
Output :
Input Arrays :
First Array : [21, 57, 11, 37, 24]
Second Array : [21, 57, 11, 37, 24]
Two Arrays Are Equal
import java.util.Arrays;
if (equalOrNot)
{
System.out.println("Two Arrays Are Equal");
}
else
{
System.out.println("Two Arrays Are Not equal");
}
}
}
Output :
Input Arrays :
First Array : [21, 57, 11, 37, 24]
Second Array : [21, 57, 11, 37, 24]
Two Arrays Are Equal
--------------------
4) Write a java program to find all pairs of elements in an integer array whose sum
is equal to a given number?
import java.util.Arrays;
System.out.println("======================================");
}
findThePairs(new int[] {4, -5, 9, 11, 25, 13, 12, 8}, 20);
findThePairs(new int[] {12, 23, 125, 41, -75, 38, 27, 11}, 50);
}
}
Output :
--------------------
5) Write a java program to find continuous sub array whose sum is equal to a given
number?
import java.util.Arrays;
int start = 0;
//Incrementing start by 1
start++;
}
if(sum == inputNumber)
{
System.out.println("Input Array : "+Arrays.toString(inputArray));
System.out.println();
}
}
System.out.println("=================================");
}
--------------
import java.util.Arrays;
import java.util.HashSet;
class MainClass
{
public static void main(String[] args)
{
String[] inputArray1 = {"ONE", "TWO", "THREE", "FOUR", "FIVE", "FOUR"};
import java.util.Arrays;
import java.util.HashSet;
class MainClass
{
public static void main(String[] args)
{
String[] inputArray1 = {"ONE", "TWO", "THREE", "FOUR", "FIVE", "FOUR"};
set1.retainAll(set2);
-------------------
import java.util.Arrays;
//Initializing counter to 0
int counter = 0;
if(inputArray[i] != 0)
{
//Assigning inputArray[i] to inputArray[counter]
inputArray[counter] = inputArray[i];
counter++;
}
}
counter++;
}
System.out.println(Arrays.toString(inputArray));
System.out.println("======================================");
}
import java.util.Arrays;
if(inputArray[i] != 0)
{
//Assigning inputArray[i] to inputArray[counter]
inputArray[counter] = inputArray[i];
counter--;
}
System.out.println(Arrays.toString(inputArray));
System.out.println("====================================");
}
------------------
https://javaconceptoftheday.com/how-to-find-all-the-leaders-in-an-integer-array/
Given an integer array, you have to find all the leader elements in this array. An
element is said to be leader if all the elements on it’s right side are smaller
than it. Rightmost element is always a leader. For example, if {14, 9, 11, 7, 8, 5,
3} is the given array then {14, 11, 8, 5, 3} are the leaders in this array.
System.out.println(inputArray[inputArrayLength-1]);
System.out.println(inputArray[i]);
max = inputArray[i];
}
}
}
Output :
break;
}
}
if (flag)
{
System.out.println(inputArray[i]);
}
}
}
------------
9) Write a java program to find a missing number in an integer array?
import java.util.Arrays;
return sum;
}
--------
10) Write a java program to convert an array to ArrayList and an ArrayList to
array?
Array To ArrayList :
import java.util.ArrayList;
import java.util.Arrays;
System.out.println(list);
}
}
ArrayList To Array :
import java.util.ArrayList;
list.add("JAVA");
list.add("JSP");
list.add("ANDROID");
list.add("STRUTS");
list.add("HADOOP");
list.add("JSF");
list.toArray(array);
import java.util.Arrays;
import java.util.HashMap;
elementCountMap.put(i, elementCountMap.get(i)+1);
}
else
{
//If element is not present in elementCountMap,
//adding this element to elementCountMap with 1 as it's value
elementCountMap.put(i, 1);
}
}
-----------
12) Write a java program to reverse an array without using an additional array?
import java.util.Arrays;
int temp;
inputArray[i] = inputArray[inputArray.length-1-i];
inputArray[inputArray.length-1-i] = temp;
}
System.out.println("=========================================");
}
Output :
o/p:
Original array:
1 2 3 4 5
Array in reverse order:
5 4 3 2 1
---------------
Given an unsorted array of integers, print the array after removing the duplicate
elements from it. We need to print distinct array elements according to their first
occurrence.
Examples:
Input: arr[] = { 1, 2, 5, 1, 7, 2, 4, 2}
Output: 1 2 5 7 4
Explanation: {1, 2} appear more than one time.
Input: arr[] = { 3, 3, 4, 1, 1}
Output: 3 4 1
• Take a Set
• Insert all array element in the Set. Set does not allow duplicates and sets
like LinkedHashSet maintains the order of insertion so it will remove duplicates
and elements will be printed in the same order in which it is inserted.
• Convert the formed set into array.
• Print elements of Set.
import java.util.*;
class GFG {
// Driver code
public static void main(String[] args)
{
int arr[] = { 1, 2, 5, 1, 7, 2, 4, 2 };
removeDuplicates(arr);
}
}
import java.util.Arrays;
public class RemoveDuplicateInArrayExample3{
public static int removeDuplicateElements(int arr[], int n){
if (n==0 || n==1){
return n;
}
int[] temp = new int[n];
int j = 0;
for (int i=0; i<n-1; i++){
if (arr[i] != arr[i+1]){
temp[j++] = arr[i];
}
}
temp[j++] = arr[n-1];
// Changing original array
for (int i=0; i<j; i++){
arr[i] = temp[i];
}
return j;
}
o/p:
10 20 30 40 50 70 90
--------------
14) Write a java program to find union and intersection of multiple arrays?
import java.util.Arrays;
import java.util.HashSet;
System.out.println("======================");
System.out.println("===========================");
System.out.println("===========================");
System.out.println(unionSet);
}
Output :
Input Arrays :
======================
[2, 3, 4, 7, 1]
[4, 1, 3, 5]
[8, 4, 6, 2, 1]
[7, 9, 4, 1]
===========================
Union Of All Input Arrays :
===========================
[1, 2, 3, 4, 5, 6, 7, 8, 9]
import java.util.Arrays;
import java.util.HashSet;
System.out.println("======================");
intersectionSet.retainAll(set);
}
System.out.println("===========================");
System.out.println("===========================");
System.out.println(intersectionSet);
}
Output :
Input Arrays :
======================
[2, 3, 4, 7, 1]
[4, 1, 3, 5]
[8, 4, 6, 2, 1]
[7, 9, 4, 1]
===========================
Intersection Of All Input Arrays :
===========================
[1, 4]
-----------
15) Write a java program to find the most frequent element in an array?
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;
elementCountMap.put(i, elementCountMap.get(i)+1);
}
else
{
//If an element is not present, put that element with 1 as its
value
elementCountMap.put(i, 1);
}
}
int element = 0;
int frequency = 1;
frequency = entry.getValue();
}
}
if(frequency > 1)
{
System.out.println("Input Array : "+Arrays.toString(inputArray));
System.out.println("========================");
}
else
{
System.out.println("Input Array : "+Arrays.toString(inputArray));
System.out.println("=========================");
}
}
Output :
1 2 8 3 2 2 2 5 1
In the given array, 1 has appeared two times so its frequency be 2 and 2 has
appeared four times so have frequency 4 and so on.
output:
----------------------------------------
Element | Frequency
----------------------------------------
1 | 2
2 | 4
8 | 1
3 | 1
5 | 1
----------------------------------------
-----------------------------------------------
import java.util.Arrays;
for (int i = 1; i < a.length; i++) // iterate for loop from arrays 1st
index (second element)
{
if (a[i] > max)
{
max = a[i];
}
if (a[i] < min)
{
min = a[i];
}
}
--------------------------------
In this example we are sorting a char array. We have demonstrated two types of
sorting in the program 1) Complete sorting using sort(char[] a) method 2) Sorting
specified range of characters only using sort(char[] a, int fromIndex, int toIndex)
method.
import java.util.Arrays;
class SortCharArray {
// Selective Sorting
/* public static void sort(char[] a, int fromIndex,
* int toIndex): Sorts the specified range of the
* array into ascending order. The range to be sorted
* extends from the index fromIndex, inclusive, to the
* index toIndex, exclusive. If fromIndex == toIndex,
* the range to be sorted is empty.
*/
Arrays.sort(charArray2, 2, 5);
Output:
**Char Array Before Sorting**
A
Q
S
Z
P
**Char Array After Sorting**
A
P
Q
S
Z
**Selective Sorting**
D
F
J
U
V
M
C
--------------
}
Output
Index position of Smallest value in a given array is : 4
---------
int a[] = new int[] { 12, 44, 23, 56, 23, 78, 13 };
Output
--------------
20. Java program to print odd and even numbers from an array
1. package com.candidjava;
2.
3. public class OddEvenArray
4. {
5. public static void main(String args[])
6. {
7. int s, i;
8.
9. int[] a = { 33, 2, 4, 71, 88, 92, 9, 1 };
10.
11. for (i = 0; i < a.length; i++)
12. {
13. for (int j = i + 1; j < a.length; j++)
14.
15. {
16. if (a[i] > a[j])
17. {
18. s = a[i];
19. a[i] = a[j];
20. a[j] = s;
21.
22. }
23.
24. }
25.
26. }
27.
28. System.out.print("Input numbers :");
29.
30. for (i = 0; i < a.length; i++)
31.
32. {
33.
34. System.out.print(" " + a[i]);
35.
36. }
37.
38. System.out.print("\nOdd numbers :");
39.
40. for (i = 0; i <= a.length - 1; i++)
41. {
42.
43. if (a[i] % 2 != 0)
44. {
45.
46. System.out.print(" " + a[i]);
47. }
48.
49. }
50.
51. System.out.print("\nEven numbers :");
52.
53. for (i = 0; i < a.length; i++)
54.
55. {
56. if (a[i] % 2 == 0)
57. {
58.
59. System.out.print(" " + a[i]);
60.
61. }
62.
63. }
64.
65. }
66.
67. }
68.
Output
Input numbers : 1 2 4 9 33 71 88 92
Odd numbers : 1 9 33 71
Even numbers : 2 4 88 92
We can print odd and even numbers from an array in java by getting remainder of
each element and checking if it is divided by 2 or not. If it is divided by 2, it
is even number otherwise it is odd number.
Odd Numbers:
1
5
3
Even Numbers:
2
6
2
In this program, we need to print the element which is present on even position.
Even positioned element can be found by traversing the array and incrementing the
value of i by 2.
//Initialize array
int [] arr = new int [] {1, 2, 3, 4, 5};
o/p:
Elements of given array present on even position:
2
4
In this program, we need to print the elements of the array which are present in
odd positions. This can be accomplished by looping through the array and printing
the elements of an array by incrementing i by 2 till the end of the array is
reached.
--------------------
package com.candidjava;
class MatrixAddition
{
public static void main(String args[])
{
int[][] a = new int[][] { { 1, 2, 3},{ 4, 5, 6},{ 7, 8, 9} };
int[][] b = new int[][] { { 10, 11, 12},{ 13, 14, 15},{ 16, 17, 18} };
int[][] c = new int[3][3];
Output
o/p:
2 6 8
4 8 6
4 6 9
---------------
22. Java program to check given matrix is null matrix
package com.candidjava;
class NullMatrix
{
public static void main(String args[])
{
int[][] a = new int[][] { { 0, 0, 0},{ 0, 0, 1},{ 0, 0, 0} };
boolean setValue = true;
if(setValue == true)
{
System.out.println("The Given Matrix is a Null Matrix");
}
else
{
System.out.println("The Given Matrix is not a Null Matrix");
}
}
}
Output
The Given Matrix Value:
0 0 0
0 0 1
0 0 0
The Given Matrix is not a Null Matrix
----------
if(setValue == true)
{
System.out.println("The Given Matrix is a Diagonal Matrix");
}
else
{
System.out.println("The Given Matrix is not a Diagonal Matrix");
}
}
}
Output
The Given Matrix Value:
1 0 0
0 3 0
0 0 3
The Given Matrix is a Diagonal Matrix
-------------
class PrintLargest
{
/* Function to print three largest elements */
static void print3largest(int arr[], int arr_size)
{
int i, first, second, third;
Output :
Three largest elements are 34 13 12
Another approach:An efficient way to solve this problem is to use any O(nLogn)
sorting algorithm & simply returning the last 3 largest elements .
import java.io.*;
import java.util.Arrays;
class GFG {
void find3largest(int[] arr)
{
Arrays.sort(arr); //It uses Tuned Quicksort with
//avg. case Time complexity = O(nLogn)
int n = arr.length;
int check = 0, count = 1;
if(count<4){
if(check!=arr[n-i])
{
// to handle duplicate values
System.out.print(arr[n-i]+" ");
check = arr[n-i];
count++;
}
}
else
break;
}
// Driver code
public static void main(String[] args)
{
GFG obj = new GFG();
int[] arr={12,45,1,-1,45,54,23,5,0,-10};
obj.find3largest(arr);
}
Output :
54 45 23
In this program, we need to find out the largest element present in the array and
display it. This can be accomplished by looping through the array from start to end
by comparing max with all the elements of an array. If any of element is greater
than max, then store a value of the element in max. Initially, max will hold the
value of the first element. At the end of the loop, max represents the largest
element in the array.
//Initialize array
int [] arr = new int [] {25, 11, 7, 75, 56};
//Initialize max with first element of array.
int max = arr[0];
//Loop through the array
for (int i = 0; i < arr.length; i++) {
//Compare elements of array with max
if(arr[i] > max)
max = arr[i];
}
System.out.println("Largest element present in given array: " + max);
}
}
Largest element present in given array: 75
In this program, we need to find out the smallest element present in the array.
This can be achieved by maintaining a variable min which initially will hold the
value of the first element. Loop through the array by comparing the value of min
with elements of the array. If any of the element's value is less than min, store
the value of the element in min.
//Initialize array
int [] arr = new int [] {25, 11, 7, 75, 56};
//Initialize min with first element of array.
int min = arr[0];
//Loop through the array
for (int i = 0; i < arr.length; i++) {
//Compare elements of array with min
if(arr[i] <min)
min = arr[i];
}
System.out.println("Smallest element present in given array: " + min);
}
}
o/p:
---------
25. Program to copy all elements of one array into another array
In this program, we need to copy all the elements of one array into another. This
can be accomplished by looping through the first array and store the elements of
the first array into the second array at the corresponding position.
System.out.println();
-------------
26. Program to left rotate the elements of an array
In this program, we need to rotate the elements of an array towards the left by the
specified number of times. In the left rotation, each element of the array will be
shifted to its left by one position and the first element of the array will be
added to end of the list. This process will be followed for a specified number of
times.
class RotateLeft {
public static void main(String[] args) {
//Initialize array
int [] arr = new int [] {1, 2, 3, 4, 5};
//n determine the number of times an array should be rotated
int n = 3;
//Displays original array
System.out.println("Original array: ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
//Rotate the given array by n times toward left
for(int i = 0; i < n; i++){
int j, first;
//Stores the first element of the array
first = arr[0];
for(j = 0; j < arr.length-1; j++){
//Shift element of array by one
arr[j] = arr[j+1];
}
//First element of array will be added to the end
arr[j] = first;
}
System.out.println();
//Displays resulting array after rotation
System.out.println("Array after left rotation: ");
for(int i = 0; i< arr.length; i++){
System.out.print(arr[i] + " ");
}
}
}
output:
Original Array:
1 2 3 4 5
Array after left rotation:
4 5 1 2 3
---------------
This is a simple program to create an array and then to print it's all elements.
Arrays are the special variables that store multiple values under the same name in
the contiguous memory allocation. Elements of the array can be accessed through
their indexes.
o/p:
Elements of given array:
1 2 3 4 5
o/p:
Number of elements present in given array: 5
---------
28. Java Program to print the sum of all the items of the array
In this program, we need to calculate the sum of all the elements of an array. This
can be solved by looping through the array and add the value of the element in each
iteration to variable sum.
Java Program to print the sum of all the items of the array
Sum of all elements of an array is 1 + 2 + 3 + 4 + 5 = 15.
o/p:
Sum of all the elements of an array: 15
-----------------------------
In this program, we need to rotate the elements of array towards its right by the
specified number of times. An array is said to be right rotated if all elements of
the array are moved to its right by one position. One approach is to loop through
the array by shifting each element of the array to its next position. The last
element of the array will become the first element of the rotated array.
Algorithm
STEP 1: START
STEP 2: INITIALIZE arr[] ={1, 2, 3, 4, 5 }.
STEP 3: SET n =3
STEP 4: PRINT "Original Array"
STEP 5: REPEAT STEP 6 UNTIL i<arr.length
//for(i=0; i<arr.length; i++)
STEP 6: PRINT arr[i]
STEP 7: REPEAT STEP 8 to STEP 12 UNTIL i<n
// for(i=0; i<n; i++ )
STEP 8: DEFINE j, last.
STEP 9: last = arr[arr.length-1]
STEP 10: REPEAT STEP 11 UNTIL j>0
//for(j= arr.length-1;j>0; j--)
STEP 11: arr[j]= arr[j-1]
STEP 12: arr[0]= last
STEP 13: PRINT "Array after right rotation"
STEP 14: REPEAT STEP 15 UNTIL i<arr.length
//for(i=0; i<arr.length; i++)
STEP 15: PRINT arr[i]
STEP 16: END
class RotateRight {
public static void main(String[] args) {
//Initialize array
int [] arr = new int [] {1, 2, 3, 4, 5};
//n determine the number of times an array should be rotated.
int n = 3;
System.out.println();
o/p:
Original Array:
1 2 3 4 5
Array after right rotation:
3 4 5 1 2
--------------
//Initialize array
int [] arr = new int [] {5, 2, 8, 7, 1};
int temp = 0;
System.out.println();
o/p:
Elements of original array:
5 2 8 7 1
Elements of array sorted in ascending order:
1 2 5 7 8
----------------
System.out.println();
----------------
32. Java Program to find Third Largest Number in an Array
We can find the third largest number in an array in java by sorting the array and
returning the 3nd largest number. Let's see the full example to find the third
largest number in java array.
o/p:
Third Largest:3
Third Largest:66
o/p:
Second Largest: 5
Second Largest: 77
import java.util.Arrays;
public class SecondLargestInArrayExample1{
public static int getSecondLargest(int[] a, int total){
Arrays.sort(a);
return a[total-2];
}
public static void main(String args[]){
int a[]={1,2,5,6,3,2};
int b[]={44,66,99,77,33,22,55};
System.out.println("Second Largest: "+getSecondLargest(a,6));
System.out.println("Second Largest: "+getSecondLargest(b,7));
}}
o/p:
Second Largest: 5
Second Largest: 77
import java.util.*;
public class SecondLargestInArrayExample2{
public static int getSecondLargest(Integer[] a, int total){
List<Integer> list=Arrays.asList(a);
Collections.sort(list);
int element=list.get(total-2);
return element;
}
public static void main(String args[]){
Integer a[]={1,2,5,6,3,2};
Integer b[]={44,66,99,77,33,22,55};
System.out.println("Second Largest: "+getSecondLargest(a,6));
System.out.println("Second Largest: "+getSecondLargest(b,7));
}}
o/p:
Second Largest: 5
Second Largest: 77
o/p:
Largest: 6
Largest: 99
---------------
o/p:
Second smallest: 2
Second smallest: 33
import java.util.*;
public class SecondSmallestInArrayExample1{
public static int getSecondSmallest(int[] a, int total){
Arrays.sort(a);
return a[1];
}
public static void main(String args[]){
int a[]={1,2,5,6,3,2};
int b[]={44,66,99,77,33,22,55};
System.out.println("Second Smallest: "+getSecondSmallest(a,6));
System.out.println("Second Smallest: "+getSecondSmallest(b,7));
}}
o/p:
Second smallest: 2
Second smallest: 33
import java.util.*;
public class SecondSmallestInArrayExample2{
public static int getSecondSmallest(Integer[] a, int total){
List<Integer> list=Arrays.asList(a);
Collections.sort(list);
int element=list.get(1);
return element;
}
public static void main(String args[]){
Integer a[]={1,2,5,6,3,2};
Integer b[]={44,66,99,77,33,22,55};
System.out.println("Second Smallest: "+getSecondSmallest(a,6));
System.out.println("Second Smallest: "+getSecondSmallest(b,7));
}}
Second smallest: 2
Second smallest: 33
import java.util.*;
public class SmallestInArrayExample1{
public static int getSmallest(int[] a, int total){
Arrays.sort(a);
return a[0];
}
public static void main(String args[]){
int a[]={1,2,5,6,3,2};
int b[]={44,66,99,77,33,22,55};
System.out.println("Smallest: "+getSmallest(a,6));
System.out.println("Smallest: "+getSmallest(b,7));
}}
import java.util.*;
public class SmallestInArrayExample2{
public static int getSmallest(Integer[] a, int total){
List<Integer> list=Arrays.asList(a);
Collections.sort(list);
int element=list.get(0);
return element;
}
public static void main(String args[]){
Integer a[]={1,2,5,6,3,2};
Integer b[]={44,66,99,77,33,22,55};
System.out.println("Smallest: "+getSmallest(a,6));
System.out.println("Smallest: "+getSmallest(b,7));
}}
o/p:
Smallest: 1
Smallest: 22
---------------------
We can multiply two matrices in java using binary * operator and executing another
loop. A matrix is also known as array of arrays. We can add, subtract and multiply
matrices.
o/p:
6 6 6
12 12 12
18 18 18
Converting rows of a matrix into columns and columns of a matrix into row is called
transpose of a matrix.
o/p:
You can also use a method where values of matrix are not predefined. Here, user has
to put the values as input.
import java.util.Scanner;
public class MatrixTransposeExample2
{
public static void main(String args[])
{
int i, j;
System.out.println("Enter total rows and columns: ");
Scanner s = new Scanner(System.in);
int row = s.nextInt();
int column = s.nextInt();
int array[][] = new int[row][column];
System.out.println("Enter matrix:");
for(i = 0; i < row; i++)
{
for(j = 0; j < column; j++)
{
array[i][j] = s.nextInt();
System.out.print(" ");
}
}
System.out.println("The above matrix before Transpose is ");
for(i = 0; i < row; i++)
{
for(j = 0; j < column; j++)
{
System.out.print(array[i][j]+" ");
}
System.out.println(" ");
}
System.out.println("The above matrix after Transpose is ");
for(i = 0; i < column; i++)
{
for(j = 0; j < row; j++)
{
System.out.print(array[j][i]+" ");
}
System.out.println(" ");
}
}
}
Java 8:
String reverse:
import java.util.stream.Stream;
import java.util.stream.Collectors;
--
Integer[] numbers = new Integer[] { 1, 2, 1, 3, 4, 4 };
Set<Integer> allItems = new HashSet<>();
Set<Integer> duplicates = Arrays.stream(numbers)
.filter(n -> !allItems.add(n)) //Set.add() returns false if the item was
already in the set.
.collect(Collectors.toSet());
System.out.println(duplicates); // [1, 4]
System.out.println(duplicateElements);
}
--------
/**
*
* A simple Java Program to to remove duplicates from Stream in Java 8
* This example uses Stream.distinct() method to remove
* duplicates.
*/
public class Hello {
List<Integer> withDupes = Arrays.asList(10, 10, 20, 20, 30, 30, 40, 50);
}
}
Output
List with duplicates: [10, 10, 20, 20, 30, 30, 40, 50]
List without duplicates: [10, 20, 30, 40, 50]