Wednesday 11 July 2018

Java Program to find the first repeating element in array

Simple Java program to find the first repeating element in array


  • Let say there is an array
    • int array[]={8,7, 5, 3, 4, 3, 5, 6};
  • Now if you see the first repeating element is 5
  • To solve this, First think comes to our mind is  use iteration.
  • then use Set and one counter variable.
  • Since we need the first repeating element ,  do the iteration from array.length instead of 0. So that the first repeating element will come at the end in the iteration

public class FirstRepeatingElement {

public static void main(String[] args) throws java.lang.Exception {
int array[] = { 8, 7, 5, 3, 4, 3, 5, 6 };

int min = -1;

// Creates an empty hashset
HashSet<Integer> set = new HashSet<>();

// Traverse the input array from right to left
for (int i = array.length - 1; i >= 0; i--) {
// If element is already in hash set, update min
if (set.contains(array[i])) {
min = i;
}

else {
// Else add element to hash set
set.add(array[i]);
}

}

if (min != -1) {
System.out.println("The first repeating element is " + array[min]);
} else {
System.out.println("There are no repeating elements");
}

}
}


  • As you can see, Hashset will not allow duplicate, whenever it finds the duplicate element just increment the counter min variable.
  • And also it iterate the for loop in reverse direction.


Alternate approach is Use LinkedHashMap.
  • Initialize LinkedHashMap<Integer, Integer> hMap=new LinkedHashMap<>();
  • Iterate the array element in for loop
  • Check the map contains the same element already
  • if not put the element in hMap with value 1
  • if already found, then just increment the value to +1
for(int i=0;i<arr.length;i++) {
if(hMap.get(arr[i]) == null) {
hMap.put(arr[i], 1);
}else {
int val=hMap.get(arr[i]);
hMap.put(arr[i],val+1);
}
}

Finally, iterate the Map, the print the first repeating element in array.

Tuesday 19 June 2018

ToDo Sample Application Using AngularJS


ToDo Application Using AngularJS :

Building a sample ToDo application with option to add/delete and mark it as completed using following below technologies

 Angular 1.5.8, 
 HTML5,
 JavaScript,
 bootstrap 3.3.6,
 JAX-RS 2.0,
 EJB 3.2,Hibernate
 Mysql 8.0,
 Maven 3.0.3,
 Payara,
 Micro payara

For source code, download it from below github link
https://github.com/sameencse/TodoApp

Payara domain configuration:
===================
Add the below configuration

 <jdbc-connection-pool driver-classname="com.mysql.cj.jdbc.Driver" datasource-classname="com.mysql.jdbc.Driver" name="MysqlPool" res-type="java.sql.Driver">
      <property name="password" value="root"></property>
      <property name="databaseName" value="ngp"></property>
      <property name="serverName" value="127.0.0.1"></property>
      <property name="user" value="root"></property>
      <property name="portNumber" value="3306"></property>
      <property name="url" value="jdbc:mysql://localhost:3306/ngp?useSSL=false"></property>
    </jdbc-connection-pool>
 <jdbc-resource pool-name="MysqlPool" jndi-name="jdbc/todo"></jdbc-resource>
 
 <resource-ref ref="jdbc/todo"></resource-ref>
 
 Download payara micro:
 Command to Deploy on payara micro
 
 D:\server> java -cp "C:\Tools\MySQL\mysql-connector-java-8.0.11.jar;D:\server\payara-micro-4.1.2.174.jar" fish.payara.micro.PayaraMicro --deploy "C:\To
ols\project\todo-apps-ui\target\todo-apps-ui-1.0-SNAPSHOT.war" --domainConfig D:\server\domain.xml
 
 
Note: download  both the mysql-connector-java-8.0.11.jar & payara-micro-4.1.2.174.jar and modify the domain.xml file to add the JDBC connection pool


Mysql Database Setup:
===================

create database ngp;
use database ngp;

create table todo_items 
( id int PRIMARY KEY AUTO_INCREMENT,
name varchar(50) not null,
description varchar(200),
todo_date date, 
status varchar(20) not null);



DB Connection Details:
=======================
URL: jdbc:mysql://localhost:3306/ngp
Username: root
Password: root
port :3306



Sunday 8 April 2018

Arrays in Javascript


Arrays are used to store multiple values in a single variable.

Example :

var list=["apple","banana","grapes"];

As you can see the above example, values are placed inside the brackets and separated by comma(,)

even you can initialize the empty values

var list=[];

Accessing the items in Array :

       index       values
          0            apple
          1            banana
          2            grapes


 Let say , user want to access the banana item in an array then all i need to do is pass in the index value of the item I am interest in:

var selectedItem=list[1];




Adding Item to an array:


  • push
  • unshift


The push method is called directly on your array and pass the data you want to add it.
The above item will get added to your end of the arrays.

list.push("mango");

result: 

       index       values
          0            apple
          1            banana
          2            grapes
          3            mango


If you want to add the items at the beginning of the list then use the below method
list.unshift("watermelon");

result :
     index          values
          0          watermelon
          1            apple
          2            banana
          3            grapes
          4            mango

Removing items from an array:

to remove the items from an array use pop or shift method,its just the reverse of push or unshift.


var remItem=list.pop();

or 

var remFirstItem=list.unshift();


The pop method will the last item from an array and unshift method is used to remove the first item of an array.

Both the method will remove the item from an array and it will return the values of an array.




CSS Selectors

  CSS Selectors In CSS, selectors are patterns used to select the element(s) you want to style There are 3 different types of CSS selectors ...