Sunday, 22 January 2017

Write a Java Program to Find First Repeated And Non-Repeated Character from given String ?

Here we will learn how to find the first non repeated words from the string and first repeated word from the string. For example suppose we have string "teeter" in this string first non repeated word/character is "r" and first repeated word/character is "t"

Below program shows the logic to find the first non repeated word/character and first repeated word/character with multiple examples.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.util.LinkedHashMap;
import java.util.Map;

public class FirstNonRepeatedAndRepeatedCharacter {

 public static void getOutput(String str){
  Map<Character, Integer> map = new LinkedHashMap<>();
  for(char ch : str.toCharArray()){
   if(map.containsKey(ch)){
    map.put(ch, map.get(ch)+1);
   }
   else{
    map.put(ch, 1);
   }
  }
  for(char ch : str.toCharArray()){
   if(map.get(ch) == 1){
    System.out.println("First Non Repeated Character of String *"+str+"* is : "+ch);
    break;
   }
  }
  for(char ch : str.toCharArray()){
   if(map.get(ch) > 1){
    System.out.println("First Repeated Character of String *"+str+"* is : "+ch+"\n");
    break;
   }
  }
 }
 public static void main(String[] args) {
  FirstNonRepeatedAndRepeatedCharacter.getOutput("JavaConceptOfTheDay");
  FirstNonRepeatedAndRepeatedCharacter.getOutput("teeter");
  FirstNonRepeatedAndRepeatedCharacter.getOutput("stress");
 }
}

Output:

First Non Repeated Character of String *JavaConceptOfTheDay* is : J
First Repeated Character of String *JavaConceptOfTheDay* is : a

First Non Repeated Character of String *teeter* is : r
First Repeated Character of String *teeter* is : t

First Non Repeated Character of String *stress* is : t
First Repeated Character of String *stress* is : s


      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Read More »

Saturday, 21 January 2017

Write a Java Program to Check weather two Strings are Anagram or Not ?

For interview Perspective it is one of the most important question to check weather two string are Anagram or not.
Anagram means two string must same characters but in different order. For example suppose we have one String "THE EYES" and other String is "THEY SEE" ,these Strings are Anagram because both strings have same set of characters but in different order. For more example see below program .

Anagram Rules:
  • Both strings must have same length.
  • Convert both strings in uppercase or lowercase.
  • Remove all the white spaces from both strings.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
import java.util.Map;
import java.util.Arrays;
import java.util.LinkedHashMap;

public class CheckStringAnagram {
 
 //First Way to Verify Anagram using - String class contains() method
 public static void isAnagram_1(String firstStr,String secondStr){
  String first = firstStr.replaceAll("\\s", "").toLowerCase();
  String second = secondStr.replaceAll("\\s", "").toLowerCase();
  boolean status = false;
  
  if(first.length() != second.length()){
   status = false;
  }
  else{
   for(int i=0;i<first.length();i++){
    if(second.contains(String.valueOf(first.charAt(i)))){
     status = true;
    }
    else{
     status = false;
    }
   }
  }
  if(status){
   System.out.println("*"+firstStr+"* and *"+secondStr+"* are Anagram.");
  }
  else{
   System.out.println("*"+firstStr+"* and *"+secondStr+"* are Not Anagram.");
  }
 }
 //Second way to Verify Anagram using - java.util.Arrays; methods sort() and equals()
 public static void isAnagram_2(String firstStr,String secondStr){
  String first = firstStr.replaceAll("\\s", "").toLowerCase();
  String second = secondStr.replaceAll("\\s", "").toLowerCase();
  boolean status = false;
  
  if(first.length() != second.length()){
   status = false;
  }
  else{
   char[] firstArr = first.toCharArray();
   char[] secondArr = second.toCharArray();
   
   Arrays.sort(firstArr);
   Arrays.sort(secondArr);
   
   status = Arrays.equals(firstArr,secondArr);
  }
  if(status){
   System.out.println("*"+firstStr+"* and *"+secondStr+"* are Anagram.");
  }
  else{
   System.out.println("*"+firstStr+"* and *"+secondStr+"* are Not Anagram.");
  }
 }
 //Third way to Verify Anagram using - Map interface
 public static void isAnagram_3(String firstStr,String secondStr){
  String first = firstStr.replaceAll("\\s", "").toLowerCase();
  String second = secondStr.replaceAll("\\s", "").toLowerCase();
  boolean status = false;
  
  if(first.length() != second.length()){
   status = false;
  }
  else{
   Map<Character, Integer> map = new LinkedHashMap<>();
   for(int i=0;i<first.length();i++){
    if(map.containsKey(first.charAt(i))){
     map.put(first.charAt(i), map.get(first.charAt(i))+1);
    }
    else{
     map.put(first.charAt(i), 1);
    }
   }
   for(int i=0;i<second.length();i++){
    if(map.containsKey(second.charAt(i))){
     map.put(second.charAt(i), map.get(second.charAt(i))-1);
    }
    else{
     map.put(second.charAt(i), 1);
    }
   }
   for(int i : map.values()){
    if(i != 0) status = false;
    else status = true;
   }
  }
  if(status){
   System.out.println("*"+firstStr+"* and *"+secondStr+"* are Anagram.");
  }
  else{
   System.out.println("*"+firstStr+"* and *"+secondStr+"* are Not Anagram.");
  }
 }
 public static void main(String[] args) {
  CheckStringAnagram.isAnagram_1("violence", "nice");
  CheckStringAnagram.isAnagram_1("MIRACLE", "CLAIMER");
  CheckStringAnagram.isAnagram_3("THE EYES", "THEY SEE");
  CheckStringAnagram.isAnagram_1("MOTHER IN LAW", "WOMAN HITLER");
  CheckStringAnagram.isAnagram_3("School Master", "The Classroom");
  CheckStringAnagram.isAnagram_1("THAT QUEER SHAKE", "THE EARTHQUAKES");
  CheckStringAnagram.isAnagram_2("ANGELS AND DEMONS", "aaddeeglmnnnoss");
  CheckStringAnagram.isAnagram_2("Goldens and Names", "Endless God Manna");
 }
}


Output:

*violence* and *nice* are Not Anagram.
*MIRACLE* and *CLAIMER* are Anagram.
*THE EYES* and *THEY SEE* are Anagram.
*MOTHER IN LAW* and *WOMAN HITLER* are Anagram.
*School Master* and *The Classroom* are Anagram.
*THAT QUEER SHAKE* and *THE EARTHQUAKES* are Anagram.
*ANGELS AND DEMONS* and *aaddeeglmnnnoss* are Anagram.
*Goldens and Names* and *Endless God Manna* are Anagram.




      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Read More »

Thursday, 19 January 2017

Write a Java Program to remove duplicate characters from String Without Using any API ?

Here we will learn the program to remove all the duplicate characters from string ,suppose we have string "Malayalam" then output will be "Malym". In given string we have duplicate character like 'a', and 'l' , so the output will become "Malym".

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class RemoveDuplicateCharacter {

 public static void remove(String str){
  String output = "";  
  String duplicateChar = "";
  
  for(int i=0;i<str.length();i++){
   if(output.contains(String.valueOf(str.charAt(i)))){
    duplicateChar+= str.charAt(i);
   }
   else{
    output+= str.charAt(i);
   }
  }
  System.out.println("Original String :\t"+str+"\nDuplicate Characters :\t"+duplicateChar+"\nOutput String :\t\t"+output);
 }
 public static void main(String[] args) {
  RemoveDuplicateCharacter.remove("Pushkarsa");
  System.out.println();
  RemoveDuplicateCharacter.remove("Malayalam");
 }

}

Output:

Original String : Pushkarsa
Duplicate Characters : sa
Output String : Pushkar

Original String : Malayalam
Duplicate Characters : aala
Output String : Malym


      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Read More »

Write a Java Program to remove duplicate characters from String using any API ?

Here we will learn the program to remove all the duplicate characters from string ,suppose we have string "Malayalam" then output will be "Mym". In given string we have duplicate character like 'a', and 'l' , so the output will become "Mym".

In this program we have used LinkedHashMap to maintain character insertion order and to find the duplicate character. And we have store character as key in Map to find duplicate character.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

public class RemoveDuplicateCharacter {
 
 public static void removeWithCollection(String str){
  Map<Character, Integer> map = new LinkedHashMap<>();
  
  for(int i=0;i<str.length();i++){
   if(map.containsKey(str.charAt(i))){
    map.put(str.charAt(i), map.get(str.charAt(i))+1);
   }
   else{
    map.put(str.charAt(i), 1);
   }
  }
  String output = "";
  String duplicateChar = "";
  for(Entry<Character, Integer> en : map.entrySet()){
   if(map.get(en.getKey()) == 1){
    output+=en.getKey();
   }
   else{
    duplicateChar+=en.getKey();
   }
  }
  System.out.println("Original String :\t"+str+"\nDuplicate Characters :\t"+duplicateChar+"\nOutput String :\t\t"+output);
 }
 public static void main(String[] args) {
  RemoveDuplicateCharacter.removeWithCollection("Pushkar Khosla");
  System.out.println();
  RemoveDuplicateCharacter.removeWithCollection("Malayalam");
 }
}

Output:

Original String :             Pushkar Khosla
Duplicate Characters : sha
Output String :             Pukr Kol

Original String :             Malayalam
Duplicate Characters : al
Output String :              Mym



      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Read More »

What is Method References In Java ?


Method References :

Method References help us to point to methods by their names,A method references is described using ::(double colon) symbol.

It is used to point the following types of method : 
  1. static methods
  2. instance methods 
  3. constructor with new operator
Example Showing Method References:

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

public class MethodReferenceExample {

public static void main(String[] args) {

List<String> list = new ArrayList<String>();
list.add("D");
list.add("A");
list.add("C");
list.add("B");
list.forEach(System.out::println);
}

}
Program Output :

D
A
C
B



      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Read More »