Menu
Topics Index
...
`

Find If A List Has Duplicates


If you need explanation Read this topic

If you need Answer Take test on this topic

Write a program to find if a list has duplicates.

Input (Strings List) Output (Boolean - whether list has duplicates or not)
["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "May", "Jan"] true
["Jan", "Jan", "Jan", "Jan", "Jan", "Jan"] true
["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul"] false
["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"] false


import java.util.*;
class FindIfListHasDuplicates
{
    public static void main(String s[])
    {
        List<String> months = new ArrayList<String>();
        months.add("Jan");
        months.add("Feb");
        months.add("Mar");
        months.add("Apr");
        months.add("May");
        months.add("Jun");
        months.add("Jul");
        months.add("May");
        months.add("Feb");
        
        boolean result = hasDuplicates(months);
        
        if(result)
        {
            System.out.println("The list " + months + " has duplicates.");
        }
        else
        {
            System.out.println("The list " + months + " has no duplicates.");
        }

    }


    public static boolean hasDuplicates(List<String> inputList)
    {
    }

}
Topic: Java ArrayList

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

in this program we need to check duplicate values present or not.so for that we need 2 for loops.outer for loop i=0;i<length of list ;i++....thn inner for loop j=0;j<length of list;j++.inside inner for loop use if condition (i!=j) plus to compare outer and inner for loop 's list values.if it both the values equal thn return true and break the for loop control.else wait upto all comparison thn finally return false

Posted by Maheshwari Natarajan    2014-12-11 11:18:05


for checking duplicate values of a list.... first we need to take two loops.. first inner loop is for selectng each elemnt of string one by one.. den inner loop will be for comparing each elememt of list with th selected one... if duplicate found then print true else false... ........frstly take a loop i=1 to size(list) den take temporary string variable str =list[i].... den take inner loop j=i 1 to list size..... n chek whether list(j) equals str if yes den return true...... exit loops return false

Posted by Asma Mujtaba Khan    2014-12-11 11:22:36


take an for loop put condition i<list.size() dont increase the i value here..list.size() will decrease here..
* get the String into temp variable using get(i)
*remove the the String at location i using the remove(i)
* now check whether the removed String is present or not...using contains(temp) if it is contains return true...
*here i always 0 so every time we are taking the 0th location element and checking
repeat this process until it reaches the size..
return false after coming out of the loop bcz if find duplicate we are returning in loop only..coming out of the loop means we did not find any duplicate elements so return false

Posted by Uday Kumar    2014-12-12 02:17:56


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Maheshwari Natarajan, Asma Mujtaba Khan,  for 'Second Correct Comment'. The 'lucky liker' is Bolo Raven. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2014-12-14 05:43:48


© meritcampus 2019

All Rights Reserved.

Open In App