Posts

Showing posts from August, 2024

Print unique element

  import java.util.HashMap ; public class print_unique_element { public static void main (String[] args) { int arr[] = { 1 , 2 , 5 , 3 , 3 , 1 , 7 , 7 } ; HashMap<Integer , Integer> hm = new HashMap<Integer , Integer>() ; for ( int val:arr){ if (!hm.containsKey(val)){ hm.put(val , 1 ) ; } else { hm.put(val , hm.get(val)+ 1 ) ; } } hm.forEach((key , value)-> { if (value== 1 ){ System. out .println(key) ; } }) ; } }

sum_integer_element_in_array

  public class sum_integer_element_in_array { public static void main (String[] args) { String arr[] = { "1" , "e" , "4" , "&" , "9" } ; int sum= 0 ; for (String val:arr){ try { sum =sum+Integer. parseInt (val) ; } catch (NumberFormatException e){ } } System. out .println(sum) ; } }

Largest and smallest number in array

  public class find_largest_smallest_number { public static void main (String[] args) { int arr[] = { 23 , 2 , 3 , 43 , 67 , 12 , 9 } ; int max=arr[ 0 ] ; int min=arr[ 0 ] ; for ( int num: arr){ if (num<min){ min=num ; } if (num>max){ max=num ; } } System. out .println( "Min=" +min+ " & " + "Max=" +max) ; } }

Common elements in array - java

  import java.util.HashSet ; import java.util.Set ; public class common_element_array { public static void main (String[] args) { int arr1[] = { 2 , 1 , 5 , 4 , 9 } ; int arr2[] = { 1 , 3 , 7 , 5 , 9 } ; //Method 1 Set<Integer> set1 = new HashSet<Integer>() ; Set<Integer> common = new HashSet<Integer>() ; for (Integer arr: arr1){ set1.add(arr) ; } for (Integer arr3:arr2){ if (set1.contains(arr3)){ common.add(arr3) ; } } System. out .println(common) ; } }

Input: AABBCCCDD , Output A2B2C3D2

public class a2b2c3d2 { public static void main (String[] args) { String value = "aabbcccdd" ; String newValue= "" ; int count= 1 ; StringBuilder result = new StringBuilder() ; for ( int i= 0 ; i<value.length() ; i++){ if (i+ 1 <value.length() && value.charAt(i)==value.charAt(i+ 1 )){ count++ ; } else { result.append(value.charAt(i)).append(count) ; count = 1 ; } } System. out .println(result) ; } }

Count type of character

  public class count_type_of_character { public static void main (String[] args) { String s = "I@Love*13" ; int upper= 0 , lower= 0 , special= 0 , number= 0 ; char arr[] = s.toCharArray() ; for ( int i= 0 ; i<s.length() ; i++){ if (arr[i]>= 'a' && arr[i]<= 'z' ){ lower++ ; } else if (arr[i]>= 'A' && arr[i]<= 'Z' ) { number++ ; } else if (arr[i]>= '0' && arr[i]<= '9' ) { upper++ ; } else { special++ ; } } System. out .println( "lower " +lower) ; System. out .println( "upper " +upper) ; System. out .println( "special " +special) ; System. out .println( "number " +number) ; } }

Missing Numbers

  public class missing_numbers { public static void main (String[] args) { int arr[] = { 2 , 7 , 9 } ; boolean flag= false; for ( int i=arr[ 0 ] ; i<arr[arr. length - 1 ] ; i++){ for ( int j= 0 ; j<arr. length ; j++){ if (i==arr[j]){ flag= true; break; } else { flag= false; } } if (!flag){ System. out .println(i) ; } } } }

Fibonacci Series

  public class fibonocci { public static void main (String[] args) { int n1= 0 , n2= 1 ; int n3 ; int num= 10 ; System. out .print(n1+ " " +n2+ " " ) ; for ( int i= 2 ; i<num ; i++){ n3=n1+n2 ; System. out .print(n3+ " " ) ; n1=n2 ; n2=n3 ; } } }

factorial of a number

using recursion  public class test1 { static int factorial ( int num){ if (num== 0 ){ return 1 ; } else return num * factorial (num- 1 ) ; } public static void main (String[] args) { System. out .println( factorial ( 5 )) ; } } using for loop   public class factorial { public static void main (String[] args) { int fact= 1 ; for ( int i= 1 ; i< 6 ; i++){ fact = fact*i ; } System. out .println(fact) ; } }

count the occurence/duplicate of word in the given string - Java

import java.util.HashMap ; import java.util.Map ; public class test3 { public static void main (String[] args) { String s1 = "hare krishn hare krishn krishn krishn hare hare. hare ram hare ram ram ram hare hare" ; String s2 = s1.replace( "." , "" ) ; String arr[]=s2.split( " " ) ; Map<String , Integer> hm = new HashMap<String , Integer>() ; for (String val: arr){ if (hm.containsKey(val)){ hm.put(val , hm.get(val)+ 1 ) ; } else { hm.put(val , 1 ) ; } } hm.forEach((a , b)-> { if (a.equals( "ram" )) System. out .println(a+ "" +b) ; }) ; //for specific key // to print complete map -> System.out.println(hm); } }