List<Integer> list = Arrays.asList(1,2,2,6,7,8,4,3);
//Using a Set
Set<Integer> distict = new HashSet<>(list);
System.out.println("Using a Set =================>");
for(int i : distict) {
System.out.println( i + ":" + Collections.frequency(list, i));
}
System.out.println("Using a Set =================>");
//Using a Set
//Using a Map
Map<Integer, Integer> frequencyMap = new HashMap<>();
for(int i: list) {
Integer count = frequencyMap.get(i);
if (count == null) {
count = 0;
}
frequencyMap.put(i, count + 1);
}
System.out.println("Using a Map =================>");
for(Map.Entry<Integer, Integer> entry: frequencyMap.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
System.out.println("Using a Map =================>");
//Using a Map
//Using Streams
Map<Integer, Long> streamFrequencyMap = list.stream().collect(Collectors.groupingBy(Function.identity(),Collectors.counting()));
for (Map.Entry<Integer,Long> entry: streamFrequencyMap.entrySet()) {
System.out.println(entry.getKey() +": "+entry.getValue());
}
//Using Streams
Programmers Choice
Wednesday, August 10, 2022
Count Frequency of Elements in a list using Set, Map and Streams
Friday, January 10, 2020
Write a function to get the intersection point of two Linked Lists
package programmers.choice.linkedlist;
import java.util.HashSet;
class LinkedListIntersect {
public static void main(String[] args)
{
// list 1
Node n1 = new Node(1);
n1.next = new Node(2);
n1.next.next = new Node(3);
n1.next.next.next = new Node(4);
n1.next.next.next.next = new Node(5);
n1.next.next.next.next.next = new Node(6);
n1.next.next.next.next.next.next = new Node(7);
// list 2
Node n2 = new Node(10);
n2.next = new Node(9);
n2.next.next = new Node(8);
n2.next.next.next = n1.next.next.next;
Print(n1);
Print(n2);
System.out.println(MegeNode(n1, n2).data);
}
// function to print the list
public static void Print(Node n)
{
Node cur = n;
while (cur != null) {
System.out.print(cur.data + " ");
cur = cur.next;
}
System.out.println();
}
// function to find the intersection of two node
public static Node MegeNode(Node n1, Node n2)
{
// define hashset
HashSet<Node> hs = new HashSet<Node>();
while (n1 != null) {
hs.add(n1);
n1 = n1.next;
}
while (n2 != null) {
if (hs.contains(n2)) {
return n2;
}
n2 = n2.next;
}
return null;
}
}
Tuesday, June 16, 2015
Difference between Bitwise OR and Exclusive OR
Difference between Bitwise OR and Exclusive OR
Bitwise Or
Bitwise Or
- Takes 2 patterns of equal length and performs the logical inclusive OR operation
- forces a bit to be set ON in a sequence where corresponding mask bit is one
- Example
- 0101(5)
- 0011(3)
- 0111(7) // Output
- Returns XOR logical Exclusive OR outputs true when both inputs differ
- Truth Table
- 0 0 0
- 0 1 1
- 1 0 1
- 1 1 1
Swap 2 Numbers with Exclusive Or
Here is a simple program to swap to numbers with Exclusive OR
Output :
5
4
public class Swap {
public static void main(String args[])
{
int a=4;
int b=5;
a^=b;
b^=a;
a^=b;
System.out.println(a);
System.out.println(b);
}
}
Output :
5
4
Java - Division by Floating Zero
An amazing result of Java Program on Division by 0.0
Infinity means a positive but effectively infinite result that is greater than the largest number that can be stored in double
public class Fruits {
public static void main(String args[])
{
int numOranges=5;
int numApples=10;
double averageFruit=0.0;
double fruitTypes=0.0;
averageFruit=(numOranges+numApples)/fruitTypes;
System.out.println("Average fruit is "+averageFruit);
}
}
Output : Average fruit is Infinity
Infinity means a positive but effectively infinite result that is greater than the largest number that can be stored in double
Tuesday, November 18, 2014
Mysql Puzzle(Left Join)-Take out those department who have no employees assigned to it
EMP
EMPNO ENAME DEPTNO
DEPT
DEPTNO DNAME
1st Way using Subquery
SELECT D.DNAME
FROM DEPT D
WHERE
NOT EXISTS (SELECT * FROM EMP E WHERE D.DEPTNO = E.DEPTNO)
2nd way using Left Join
SELECT D.DNAME
FROM DEPT D
LEFT JOIN EMP E ON D.DEPTNO = E.DEPTNO
WHERE E.DEPTNO IS NULL
Sunday, November 16, 2014
HTML - HTTP Request Types
1) GET:- Used when the client is requesting a resource on the Web server.
2) HEAD:- Used when the client is requesting some information about a resource but not requesting the resource itself.
3) POST:- Used when the client is sending information or data to the server—for example, filling out an online form (i.e. Sends a large amount of complex data to the Web Server).
POST sends data to a specific URI and expects the resource at that URI to handle the request. The web server at this point can determine what to do with the data in the context of the specified resource.
Example can be
4) PUT:- Used when the client is sending a replacement document or uploading a new document to the Web server under the request URL.
PUT puts a file or resource at a specific URI, and exactly at that URI. If there's already a file or resource at that URI, PUT replaces that file or resource. If there is no file or resource there, PUT creates one.
Example Can be
5) DELETE:- Used when the client is trying to delete a document from the Web server, identified by the request URL.
6) TRACE:- Used when the client is asking the available proxies or intermediate servers changing the request to announce themselves.
7) OPTIONS:- Used when the client wants to determine other available methods to retrieve or process a document on the Web server.
8) CONNECT:- Used when the client wants to establish a transparent connection to a remote host, usually to facilitate SSL-encrypted communication (HTTPS) through an HTTP proxy.
2) HEAD:- Used when the client is requesting some information about a resource but not requesting the resource itself.
3) POST:- Used when the client is sending information or data to the server—for example, filling out an online form (i.e. Sends a large amount of complex data to the Web Server).
POST sends data to a specific URI and expects the resource at that URI to handle the request. The web server at this point can determine what to do with the data in the context of the specified resource.
Example can be
obj.set_attribute(value)
4) PUT:- Used when the client is sending a replacement document or uploading a new document to the Web server under the request URL.
PUT puts a file or resource at a specific URI, and exactly at that URI. If there's already a file or resource at that URI, PUT replaces that file or resource. If there is no file or resource there, PUT creates one.
Example Can be
obj.attribute = value
5) DELETE:- Used when the client is trying to delete a document from the Web server, identified by the request URL.
6) TRACE:- Used when the client is asking the available proxies or intermediate servers changing the request to announce themselves.
7) OPTIONS:- Used when the client wants to determine other available methods to retrieve or process a document on the Web server.
8) CONNECT:- Used when the client wants to establish a transparent connection to a remote host, usually to facilitate SSL-encrypted communication (HTTPS) through an HTTP proxy.
Subscribe to:
Posts (Atom)