Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions data_structures/arraylist/java/Find_Frequency_Element_Array.java
Original file line number Diff line number Diff line change
@@ -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<Integer , Integer> 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<Integer, Integer> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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