Sitemap

ARRAY_DAY-3

3 min readMar 12, 2026

--

Programs part-2

Q15) Finding the unique element in the array

package com.ravi.array_primitive;
import java.util.Arrays;
public class ArrayDemoEx16
{
void main()
{
int []arr = {10,20,30,40,20,10};
for(int i=0; i<arr.length; i++)
{
boolean isUnique = true; //reset the value of isUnique
for(int j=0; j<arr.length; j++)
{
if(i!=j && arr[i]==arr[j])
{
isUnique = false;
break;
}
}
if(isUnique)
{
IO.println("Unique elements are :"+arr[i]);
}
}
}
}

Q16) Find the second largest element in the array

package com.ravi.array_primitive;

import java.util.Arrays;
//second largest
public class ArrayDemoEx17
{
public static void main(String[] args)
{
int []arr = {12,90,56,34,77}; //descending order arr[]
int temp = 0;
for(int i=0; i<arr.length; i++)
{
for(int j=i+1; j<arr.length; j++)
{
if(arr[i] < arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
IO.println("Array elements in descending Order :"+Arrays.toString(
arr));
IO.println("Second largest element is :"+arr[1]);
}
}

WAP to find the 2nd largest element when the array contains duplicate elements

package com.ravi.array_primitive;
//second largest
public class ArrayDemoEx17
{
public static void main(String[] args)
{
int []arr = {12,90,56,34,77,77,90,90}; //12 34 56 77 77 90 90
int temp = 0;
for(int i=0; i<arr.length; i++)
{
for(int j=i+1; j<arr.length; j++)
{
if(arr[i] < arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
int largest = arr[arr.length -1]; //largest = 90
int secondLargest = -1;
for(int i = arr.length-2; i>=0; i--)
{
if(arr[i] != largest)
{
secondLargest = arr[i];
break; //break is compulsory
}
}
IO.println("Second largest element is :"+secondLargest);
}
}

By using the Stream API

package com.ravi.array_primitive;
import java.util.Arrays;
//second largest
public class ArrayDemoEx17
{
public static void main(String[] args)
{
int []arr = {12,90,56,34,77,77,90,90};
Arrays.stream(arr)
.distinct()
.boxed()
.sorted((a,b)-> b.compareTo(a))
.skip(1)
.findFirst().ifPresent(IO::println);
}
}

Get Aman Verma’s stories in your inbox

Join Medium for free to get updates from this writer.

WAP to find the second largest element without sorting

package com.ravi.array_primitive;

import java.util.TreeSet;
//second largest
public class ArrayDemoEx17
{
public static void main(String[] args)
{
int []arr = {12,90,56,34,77,77,90,90};
TreeSet<Integer> set = new TreeSet<>();
for(int element : arr)
{
set.add(element);
}
set.pollLast(); //will retrieve and delete the last element
IO.println("Second largest element is :"+set.last());
}
}

Q19) Two sum problem

package com.ravi.array_primitive;

public class ArrayDemoEx18
{
public static void main(String[] args)
{
int[] arr = {2, 7, 11, 6, 15, 3};
int target = 9;
for(int i=0; i<arr.length; i++)
{
for(int j=i+1; j<arr.length; j++)
{
if(arr[i] + arr[j] == target)
{
IO.println(target+" found in element
"+arr[i]+"+"+arr[j]);
}
}
}
}
}

Q20) Check if the array is sorted in non-decreasing order

package com.ravi.array_primitive;

public class ArrayDemo19
{
public static void main(String[] args)
{
int[] arr = {2, 3, 5, 5, 8, 10, 9};
IO.println(isNonDecreasing(arr));
}
public static boolean isNonDecreasing(int []arr)
{
for(int i=0; i<arr.length-1; i++)
{
if(arr[i] > arr[i+1])
{
return false;
}
}
return true;
}
}

Q21) Left rotate the array by one place

package com.ravi.array_primitive;

import java.util.Arrays;
public class ArrayDemo20
{
public static void main(String[] args)
{
int[] arr = {1,2,3,4,5};
IO.println("Original Array :"+Arrays.toString(arr));
int temp = arr[0]; //temp = 1
for(int i=0; i<arr.length-1; i++)
{
arr[i] = arr[i+1];
}
arr[arr.length-1] = temp;
IO.println("After Rotation"+Arrays.toString(arr));
}
}

Q22) Move all zeros to the end

package com.ravi.array_primitive;

import java.util.Arrays;
public class ArrayDemo21
{
public static void main(String[] args)
{
int[] arr = {0, 1, 3, 0, 4, 0, 9, 5};
IO.println("Original Array is :"+Arrays.toString(arr));
int temp = 0;
for(int i=0; i<arr.length; i++)
{
if(arr[i] == 0)
{
for(int j=i+1; j<arr.length; j++)
{
if(arr[j] != 0)
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
break;
}
}
}
}
IO.println("After moving all the zero Array
is :"+Arrays.toString(arr));
}
}

Q23) Maximum consecutive 1’s in an array

package com.ravi.array_primitive;

public class ArrayExDemo22
{
public static void main(String[] args)
{
int[] arr = {1,1,0,1,1,1,1};
int count = 0;
int consecutive = 0;
for(int i=0; i<arr.length; i++)
{
if(arr[i] == 1)
{
count++;
if(count > consecutive)
consecutive = count;
}
else
{
count = 0;
}
}
IO.println("Total number of consecutive 1's are :"+consecutive);
}
}

--

--

Aman Verma
Aman Verma

Written by Aman Verma

📚#Software Engineer || Ai Technolgy || #ReadingIsLife || #techylife https://blog-project-one-silk.vercel.app/