Interviewinterview program javaTop 11 program in java to find things

Top 11 program in java to find things

Write a program in java to find a duplicate character.

package com.onurdesk.find.programs;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class DuplicateCharFinder {
	/**
	 * @author Onurdesk
	 *
	 *         Thanks for visiting https://onurdesk.com like us on facebook :
	 *         https://facebook.com/onurdesk
 */
	public void findIt(String str) {
		Map<Character, Integer> baseMap = new HashMap<Character, Integer>();
		char[] charArray = str.toCharArray();
		for (Character ch : charArray) {
			if (baseMap.containsKey(ch)) {
				baseMap.put(ch, baseMap.get(ch) + 1);
			} else {
				baseMap.put(ch, 1);
			}
		}
		Set<Character> keys = baseMap.keySet();
		for (Character ch : keys) {
			if (baseMap.get(ch) > 1) {
				System.out.println(ch + "  is " + baseMap.get(ch) + " times");
			}
		}
	}

	public static void main(String a[]) {
		DuplicateCharFinder dcf = new DuplicateCharFinder();
		dcf.findIt("India is my country");
		System.out.println("Thanks for visiting https://onurdesk.com");

	}
}
program in java to find

Write a program to find duplicate numbers in java.

package com.onurdesk.find.programs;

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

public class FindDulicateNumbersInList {
	/**
	 * @author Onurdesk
	 *
	 *         Thanks for visiting https://onurdesk.com like us on facebook :
	 *         https://facebook.com/onurdesk
 */

	public static void main(String[] args) {

		List<Integer> list = new ArrayList<>();

		for (int i = 0; i < 10; i++) {
			list.add(i);
		}
		list.add(5);
		list.add(2);
		findDulicataes(list);
		System.out.println();
		System.out.println("Thanks for visiting https://onurdesk.com");

	}

	private static List<Integer> findDulicataes(List<Integer> list) {
		List<Integer> listWithoutDuplicates = new ArrayList<>();
		System.out.println("duplicate list is " + list);
		System.out.println("dulicate elements are  ");
		for (int i : list) {
			if (listWithoutDuplicates.contains(i)) {
				System.out.print(i + " ");
			} else {
				listWithoutDuplicates.add(i);
			}
		}

		return listWithoutDuplicates;
	}

}
program in java to find

Write a program to find a duplicate character in string.

package com.onurdesk.find.programs;

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

class FindDuplicats {
	/**
	 * @author Onurdesk
	 *
	 *         Thanks for visiting https://onurdesk.com like us on facebook :
	 *         https://facebook.com/onurdesk
 */
	public static void dupChar(String s) {
		Map<Character, Integer> map = new HashMap<>();
		char[] ch = s.toCharArray();
		for (char c : ch) {
			if (map.containsKey(c)) {
				map.put(c, map.get(c) + 1);
			} else {
				map.put(c, 1);
			}
		}
		Set<Character> set = map.keySet();
		for (char c1 : set) {
			if (map.get(c1) > 1) {
				System.out.println(c1 + " is " + map.get(c1) + " times");
			}
		}

	}
}

public class FindDuplicatesCharInString {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter any String Here ");
		String str = sc.nextLine();
		FindDuplicats fd = new FindDuplicats();
		fd.dupChar(str);
		System.out.println("Thanks for visiting https://onurdesk.com");
              }
}
program in java to find

Write a program to find duplicate elements from an array.

package com.onurdesk.find.programs;

import java.util.HashSet;
import java.util.Set;

public class FindDuplicatesElementsFromAnArray {
	/**
	 * @author Onurdesk
	 *
	 *         Thanks for visiting https://onurdesk.com like us on facebook :
	 *         https://facebook.com/onurdesk
 */
	public static void main(String[] args) {
		int duplicates[] = { 1, 51, 1, 5, 1, 41, 51, 12 };
		Set<Integer> nonDuplicateSet = new HashSet<>();
		Set<Integer> DuplicateSet = new HashSet<>();
		for (Integer integer : duplicates) {
			if (!nonDuplicateSet.contains(integer)) {
				nonDuplicateSet.add(integer);
			} else {
				DuplicateSet.add(integer);
			}
		}
		System.out.println("NONDuplicate Elements are" + nonDuplicateSet);
		System.out.println("Duplicate Elements are" + DuplicateSet);
		System.out.println("Thanks for visiting https://onurdesk.com");

	}

}

Write a program to find factorial of number.

package com.onurdesk.find.programs;

import java.util.Scanner;

public class FindFactorial {
	/**
	 * @author Onurdesk
	 *
	 *         Thanks for visiting https://onurdesk.com like us on facebook :
	 *         https://facebook.com/onurdesk
 */
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		System.out.println("Enter a number");
		int x = s.nextInt();
		int fact = 1;
		int i;
		for (i = 1; i <= x; i++) {
			fact = fact * i;
		}
		System.out.println("factorial numner is " + fact);
		System.out.println("Thanks for visiting https://onurdesk.com");

	}
}

Write a program to find smallest and largest number in array.

package com.onurdesk.find.programs;

public class FindLargestAndSmallestNumberInArray {
	/**
	 * @author Onurdesk
	 *
	 *         Thanks for visiting https://onurdesk.com like us on facebook :
	 *         https://facebook.com/onurdesk
 */
	public static void maxValue(int[] ar) {
		int maxValue = ar[0];
		for (int i = 0; i < ar.length; i++) {
			if (ar[i] > maxValue) {
				maxValue = ar[i];
			}
		}

		System.out.println("Maximum of value is " + maxValue);
	}

	public static void minValue(int[] ar) {
		int minValue = ar[0];
		for (int i = 0; i < ar.length; i++) {
			if (ar[i] < minValue) {
				minValue = ar[i];
			}
		}
		System.out.println("Minimum of  value is " + minValue);
	}

	public static void main(String[] args) {
		int[] arr = { 2, 5, 1, 1, 52, 12, 5 };
		maxValue(arr);
		minValue(arr);
		System.out.println("Thanks for visiting https://onurdesk.com");

	}

}

Write a program to find missing number in array.

package com.onurdesk.find.programs;

public class FindMissingNumberInArray {
	/**
	 * @author Onurdesk
	 *
	 *         Thanks for visiting https://onurdesk.com like us on facebook :
	 *         https://facebook.com/onurdesk
 */
	public static void main(String[] args) {
		int n;
		int[] arr = { 1, 2, 3, 4, 5, 7 };
		n = 7;
		int excpted_output = n * ((n + 1) / 2);
		int sum = 0;
		for (int i : arr) {
			sum = sum + i;
		}
		int result = excpted_output - sum;
		System.out.println("Missing Number is " + result);
		System.out.println("Thanks for visiting https://onurdesk.com");

	}

}

Write a program to find second largest element in array.

package com.onurdesk.find.programs;

public class FIndSecondLargestElementInArray {
	/**
	 * @author Onurdesk
	 *
	 *         Thanks for visiting https://onurdesk.com like us on facebook :
	 *         https://facebook.com/onurdesk
 */
	public static void main(String[] args) {
		int[] a = { 23, 4, 55, 23, 45, 65, 223, 121, 43, 675 };
		int tot = a.length;
		System.out.println("Second largest number is " + getSecondLargest(a, tot));
		System.out.println("Thanks for visiting https://onurdesk.com");
	}

	public static int getSecondLargest(int[] a, int tot) {
		for (int i = 0; i < tot; i++) {
			for (int j = i + 1; j < tot; j++) {
				if (a[i] < a[j]) {
					int temp = a[i];
					a[i] = a[j];
					a[j] = temp;
				}
			}
		}
		return a[tot - 2];
	}

}

Write a program to find second smallest number in array.

package com.onurdesk.find.programs;

public class FindSecondSmallestNumberInArray {
	/**
	 * @author Onurdesk
	 *
	 *         Thanks for visiting https://onurdesk.com like us on facebook :
	 *         https://facebook.com/onurdesk
 */
	public static void main(String[] args) {
		int a[] = { 2, 3, 4, 5, 6, 2, 3, 561, 335, 121, 3241, 1231, 12, 1, 12 };
		int size = a.length;
		System.out.println("Second Smallest NUmber is " + getSecondSmallestNumber(a, size));
		System.out.println("Thanks for visiting https://onurdesk.com");

	}

	public static int getSecondSmallestNumber(int a[], int size) {
		for (int i = 0; i < size; i++) {
			for (int j = i + 1; j < size; j++) {
				if (a[i] < a[j]) {
					int temp = a[i];
					a[i] = a[j];
					a[j] = temp;
				}
			}
		}

		return a[size - 2];
	}

}

Write a program to find square root cube root of integer.

package com.onurdesk.find.programs;

import java.util.Scanner;

public class FindSqaureCubeSqrt {
	/**
	 * @author Onurdesk
	 *
	 *         Thanks for visiting https://onurdesk.com like us on facebook :
	 *         https://facebook.com/onurdesk
 */
	static public void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter any Integer Number ");
		int num = sc.nextInt();
		System.out.println("Square of " + num + " is " + Math.pow(num, 2));
		System.out.println("cube of " + num + " is " + Math.pow(num, 3));
		System.out.println("Square Root of " + num + " is " + Math.sqrt(num));
		System.out.println("Thanks for visiting https://onurdesk.com");
	}

}

Write a program to find sum and average of number.

package com.onurdesk.find.programs;

import java.util.Scanner;

public class FindSumAndInteger {
	/**
	 * @author Onurdesk
	 *
	 *         Thanks for visiting https://onurdesk.com like us on facebook :
	 *         https://facebook.com/onurdesk
 */
	public static void main(String[] args) {
		int a, b, sum;
		float avg;
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter First Number");
		a = sc.nextInt();
		System.out.println("Enter Second Number");
		b = sc.nextInt();
		sum = a + b;
		System.out.println("Sum is " + sum);
		avg = (float) sum / 2;
		System.out.println("Average is " + avg);
		System.out.println("Thanks for visiting https://onurdesk.com");

	}

}

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe Today

GET EXCLUSIVE FULL ACCESS TO PREMIUM CONTENT

Get unlimited access to our EXCLUSIVE Content and our archive of subscriber stories.

Exclusive content

Latest article

More article