diff --git a/data_structures/arraylist/java/Find_Frequency_Element_Array.java b/data_structures/arraylist/java/Find_Frequency_Element_Array.java new file mode 100644 index 00000000..0a2e8069 --- /dev/null +++ b/data_structures/arraylist/java/Find_Frequency_Element_Array.java @@ -0,0 +1,39 @@ +/** + * find the frequency of element in arry; + * arr = {2,33,4,4,54,3,2} + * + * output : + * 2 ->2 + * 33->1 + * 4->2 + * 54->1 + * 3->1 + */ + + +import java.util.HashMap; +import java.util.IdentityHashMap; +import java.util.Map; + +public class FrequencyOfEachElementArray { + public static void PrintFri(int arr[]){ + Map map = new HashMap<>(); + + for (int i = 0; i < arr.length; i++) { + if(map.containsKey(arr[i])){ + map.put(arr[i], map.get(arr[i]) + 1); + } + else { + map.put(arr[i], 1); + } + } + + for(Map.Entry entry: map.entrySet()){ + System.out.println(entry.getKey() + " " + entry.getValue()); + } + } + public static void main(String[] args) { + int arr[] = {21,2,4,56,5,3,2,3,2,34,}; + PrintFri(arr); + } +} diff --git a/data_structures/arraylist/java/Find_Second_Largest_Number_In_Array.java b/data_structures/arraylist/java/Find_Second_Largest_Number_In_Array.java new file mode 100644 index 00000000..46620f89 --- /dev/null +++ b/data_structures/arraylist/java/Find_Second_Largest_Number_In_Array.java @@ -0,0 +1,37 @@ +import java.util.Arrays; + +/** + * To find second highest number form the array : + * example : + * arr = { 23,45,65,4,6,7} + * output : 45 + * */ +public class SecondHighestNumberInArray { + //functin + public static void SecondLargestNum(int arr[]){ + int size = arr.length; + if(size<2){ + System.out.println("Invalid input "); + return; + } + Arrays.sort(arr); + System.out.println(arr[size-2]); + for(int i= size-2; i>=0; i--){ + if(arr[i]!=arr[size-1]) { + System.out.println(arr[i]); + return; + } + + } + System.out.println("There are no largest number "); + } + public static void main(String[] args) { + int arr[] = {4,4,4,4,4,4,4,4,4}; + //object of class + SecondHighestNumberInArray secondHigh = new SecondHighestNumberInArray(); + secondHigh.SecondLargestNum(arr); + + } +} + +//output : there are no largest numebr