Showing posts with label check-duplicate-no. Show all posts
Showing posts with label check-duplicate-no. Show all posts

Friday, February 10, 2017

WAP to find out duplicate value all duplication in Array

import java.util.HashSet;
class DuplicateNumberArray2
{
public static void main(String[] args)
    {
        String[] arr = {"xyz", "def", "mno", "xyz", "pqr", "xyz", "def"};
HashSet<String> hs=new HashSet<String>(); //HashSet does not allow duplicate Value
System.out.println("All Duplicate Elements are :");
        for (String s:arr)
        {
       if(hs.add(s)==false) //Duplicate value will not insert & return false
                System.out.println(s);            
        }
}
}

WAP to find out duplicate value all duplication in Array

import java.util.HashSet;
class DuplicateNumberArray
{
public static void main(String[] args)
{
int[] arr={1,2,3,1,10,20,30,20,1};
System.out.println("All Duplicate Elements Are : ");
HashSet<Integer> hs =new HashSet<Integer>();  //HashSet does not allow duplicate Value
for(int x:arr)
{
if(hs.add(x)==false) //Duplicate value will not insert & return false
{
System.out.println(x);
}
}
}
}

WAP to enter a number and check whether the number is duplicate or not

import java.io.Console;
public class DuplicateNumber
{
static public void main(String...alt)
{
Console c=System.console();int flag=0;
String s=c.readLine("Enter a Number : ");
label:
for(int i=0;i<s.length()-1;i++)
{
for(int j=i+1;j<s.length();j++)
{
if(s.charAt(i)==s.charAt(j))
{
flag=1;
break label;
}
}
}

if(flag==1)
System.out.println(s+" is Duplicate Number");
else
System.out.println(s+" is Unique Number");
}
}