0% found this document useful (0 votes)
2 views6 pages

Tcs Java Codes

The document contains Java coding exercises that cover various algorithms and data structures. Key topics include finding the largest and second largest elements in an array, reversing arrays and strings, checking for palindromes, counting element frequencies using HashMaps, and identifying the first non-repeating character in a string. Each exercise is accompanied by a code implementation demonstrating the solution.

Uploaded by

gajulajhansi01
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

Tcs Java Codes

The document contains Java coding exercises that cover various algorithms and data structures. Key topics include finding the largest and second largest elements in an array, reversing arrays and strings, checking for palindromes, counting element frequencies using HashMaps, and identifying the first non-repeating character in a string. Each exercise is accompanied by a code implementation demonstrating the solution.

Uploaded by

gajulajhansi01
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Coding Practice (Java)

1. Largest Element in an Array

class Solution {

public static int largest(int[] arr) {

int max=arr[0];

for(int i=1;i<[Link];i++){

if(arr[i]>max){

max=arr[i];

return max;

2. Reverse an Array

class Solution {

public void reverseArray(int arr[]) {

// code here

int left=0;

int right=[Link]-1;

while(left<right){

int temp=arr[left];

arr[left]=arr[right];

arr[right]=temp;

left++;

right--;

3. Second Largest Element


class Solution {

public int getSecondLargest(int[] arr) {

int max1=-1;

int max2=-1;

for(int num:arr){

if(num>max1){

max2=max1;

max1=num;

else if(num>max2 && num!=max1){

max2=num;

return max2;

4. Reverse a String

class Solution {

public static String reverseString(String s) {

StringBuilder sb=new StringBuilder();

for(int i=[Link]()-1;i>=0;i--){

[Link]([Link](i));

return [Link]();

[Link] Palindrome number


class Solution {

public boolean isPalindrome(int n) {

int original = n;

int reverse = 0;

while (n != 0) {

int digit = n % 10;

reverse = reverse * 10 + digit;

n = n / 10;

return original == reverse;

6. Check Palindrome String

class Solution {

boolean isPalindrome(String s) {

int left=0;

int right=[Link]()-1;

while(left<right){

if([Link](left)!=[Link](right)){

return false;

left++;

right--;

}
return true;

7. Frequency of Elements in an Array (HashMap)

import [Link];

public class Main {


public static void main(String[] args) {

int[] arr = {2, 3, 2, 5, 3, 2};

HashMap<Integer, Integer> map = new HashMap<>();

for (int num : arr) {

if ([Link](num)) {
[Link](num, [Link](num) + 1);
} else {
[Link](num, 1);
}

[Link](map);
}
}

frequency of each character in a string (e.g., “hello” → h:1, e:1,


l:2, o:1).

class Main{

public static void main(String[] args){

String str="Jhansi";

int count=0;

[Link]("Orignal string " +str);


for(int i=0;i<[Link]();i++){

char ch=[Link](i);

for(int j=0;j<[Link]();j++){

if([Link](j)==ch){

count++;

}}

[Link](ch + ": " + count);

}}

8. First Non-Repeating Character

import [Link];

public class Main {

public static char firstNonRepeating(String s) {

HashMap<Character, Integer> map = new HashMap<>();

// Count frequency

for (char ch : [Link]()) {

if ([Link](ch)) {

[Link](ch, [Link](ch) + 1);

} else {

[Link](ch, 1);

// Find first character with frequency 1

for (char ch : [Link]()) {


if ([Link](ch) == 1) {

return ch;

return '\0'; //null character

public static void main(String[] args) {

String s = "loveleetcode";

[Link](firstNonRepeating(s));

You might also like