Safik Exp-1b
Safik Exp-1b
URK23CO2029
Date of Exercise
b)
5.
Aim
To create an array to store a given number by the user. Write a java program to find out
all the search array (another input array) and its positions.
Description
Input Main Array: The user inputs numbers into a main array.
Input Search Array: The user inputs numbers into a search array.
Search Algorithm: The program checks each number in the search array and finds all
occurrences of that number in the main array.
Output: It prints each element from the search array along with its positions in the main array.
Program
import java.util.Scanner;
import java.util.ArrayList;
System.out.println("URK23CO2029");
1
23DC2002 - Object Oriented Programming Lab Reg.No. URK23CO2029
mainArray[i] = scanner.nextInt();
searchArray[i] = scanner.nextInt();
if (mainArray[j] == searchElement) {
positions.add(j);
if (positions.isEmpty()) {
2
23DC2002 - Object Oriented Programming Lab Reg.No. URK23CO2029
} else {
System.out.println();
scanner.close();
3
23DC2002 - Object Oriented Programming Lab Reg.No. URK23CO2029
Output Screenshot
Result
The given program has been Successfully executed and the desired output has been.
4
23DC2002 - Object Oriented Programming Lab Reg.No. URK23CO2029
8.
Aim
Description
This program identifies and prints duplicate characters from a given character array by using a
basic array for counting occurrences. It checks each character and maintains a separate list of
characters that appear more than once.
Program
System.out.println("URK23CO2029");
char[] charArray = {'a', 'b', 'c', 'a', 'd', 'b', 'e', 'f', 'g', 'e'};
charCounts[c]++;
if (charCounts[i] > 1) {
duplicatesFound = true;
5
23DC2002 - Object Oriented Programming Lab Reg.No. URK23CO2029
if (!duplicatesFound) {
Output Screenshot
Result
The given program has been Successfully executed and the desired output has been.
6
23DC2002 - Object Oriented Programming Lab Reg.No. URK23CO2029
10.
Aim
To perform the following operations: write a program in Java to count the occurrence of a
given character in an array and write a program in Java to return the highest occurred character
in an array.
Description
This program counts the number of times a user-specified character occurs in a given character
array. It iterates through the array and compares each element to the target character,
incrementing a counter whenever a match is found and program finds and returns the character
that occurs most frequently in a given character array. It uses an integer array to keep track of
each character's frequency and determines the character with the highest frequency.
Program
import java.util.Scanner;
System.out.println("URK23CO2029");
char[] charArray = {'h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd'};
7
23DC2002 - Object Oriented Programming Lab Reg.No. URK23CO2029
scanner.close();
int count = 0;
if (c == targetChar) {
count++;
return count;
charCounts[c]++;
int maxCount = 0;
maxCount = charCounts[i];
8
23DC2002 - Object Oriented Programming Lab Reg.No. URK23CO2029
highestOccurredChar = (char) i;
return highestOccurredChar;
Output Screenshot
Result
The given program has been Successfully executed and the desired output has been.