0% found this document useful (0 votes)
101 views

Assignment

The document contains 4 programming questions and solutions. Question 1 asks to remove duplicate characters from a string such as "Java" to return "Jav". Question 2 asks to count the most frequent character in a string like "babcabbcaab" to help pick the most common flower type. Question 3 asks to return the second smallest number in an array like {5,1,4,3}. Question 4 asks to count the number of candles of maximum height in an array like {4,4,1,3} to return 2.

Uploaded by

Vicky Vignesh
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
101 views

Assignment

The document contains 4 programming questions and solutions. Question 1 asks to remove duplicate characters from a string such as "Java" to return "Jav". Question 2 asks to count the most frequent character in a string like "babcabbcaab" to help pick the most common flower type. Question 3 asks to return the second smallest number in an array like {5,1,4,3}. Question 4 asks to count the number of candles of maximum height in an array like {4,4,1,3} to return 2.

Uploaded by

Vicky Vignesh
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Question:1

Write a program to remove all the duplicate characters from a given input String,
like, if the given String is "Java" then the output should be "Jav". The second or
further occurrence of duplicates should be removed.
Sample Input:
Java
Sample Output:
Java

Output:

package Java_Assignment;

import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Max_Occurance {


public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter the word : ");
String word = in.next().toLowerCase();
Map<Character, Integer> letters = new HashMap<>();

for (int i = 0; i < word.length(); i++) {


char key = word.charAt(i);
if (letters.containsKey(key)) {
int oldValue = letters.get(key);
int newValue = oldValue + 1;
letters.replace(key, oldValue, newValue);
} else {
letters.put(key, 1);
}
}

Integer high =
letters.values().stream().sorted(Comparator.reverseOrder()).findFirst().get();

char mostOccured = letters.keySet().stream().filter(key ->


high.equals(letters.get(key))).findFirst().get();
System.out.println("The Max occured Character : " + mostOccured);

Question:2
Ananya loves flowers and today it's her birthday. Her best friend Ayushi wants to
present her bag full of flowers. The bag can contain only one type of flower.
Ayushi
has a bunch of flowers in her garden, but due to the restriction of the bag, she
can
pick flowers of only one kind.
Ayushi is now confused which type of flowers to pick. So she decides to pick the
flowers which has maximum occurence in her garden. Your task is to help her in
deciding the type of flower she should pick.
Sample Input :
babcabbcaab
Sample

Output

package Java_Assignment;

public class Birth_Candle {


public static void main(String[] args) {
int candles[] = {4,4,1,3};
int temp = -1;
int count = 0;
for(int i =0; i<candles.length; i++) {
if(candles[i]>temp) {
temp=candles[i];
count=1;
}else if(candles[i]==temp) {
count++;
}
}
System.out.println("The result for count of Candle is "+count);
}
}

Question:3
Pranav goes to a shop to buy candles. The shopkeeper shows him candles of
different brightness level. As Pranav hates light, he wants to buy a candle with
the
minimum brightness. But his wife instructed him not to do so.
Now Pranav has to buy a candle which is just brighter than the minimum one. Can
you help him in choosing the right candle.
Sample Input:
4
5 1 4 3
Sample Output:

Output

package Java_Assignment;

import java.util.Arrays;

public class Second_min {


public static void main(String[] args){
int[] arr={5,1,4,3};
Arrays.sort(arr);
System.out.println("The value with "+"'"+arr[1]+"'"+" is just brighter than the
minimum one");
}
}

Question:4
You are in charge of the cake for a child's birthday. You have decided the cake
will
have one candle for each year of their total age. They will only be able to blow
out
the tallest of the candles. Count how many candles are tallest.
Example:
candle = [4,4,1,3]
The maximum height candles are 4 units high. There are 2 of them, so return 2.
Sample Input:

4
3 2 1 3
Sample Output:
2

Output

package Java_Assignment;

import java.util.ArrayList;
import java.util.List;

public class Duplicate_Char {

public static void main(String[] args) {


List <String> nameList = new ArrayList<>();
nameList.add("j");
nameList.add("a");
nameList.add("v");
nameList.add("a");
System.out.println("After removing duplicate Character : ");

nameList.stream().distinct().forEach(System.out::print);
}

You might also like