Find Duplicates in a Array / Line / File

Now let us answer the below questions asked in  various java  interview questions  which are given below
1. How to find and remove duplicates in a Array / ArrayList
2. How to find and print duplicate words or lines in a File
3. How to find duplicate words in a  line of text
4. Program to find duplicates with number of occurrenceNow let us see how to solve the above tasks in 4 ways
1. Using HashSet
2. Using HashMap
3. Using ArrayList
4. Without using above

1. How to find and remove duplicates in a Array / ArrayList using Set (HashSet)

          Set never allows duplicate values . When we add a value from the array to Hashset using add method , if the value does not exist in the HashSet , then the value is added to Hashset and the add method returns the result true . If the the value is already exist in the HashSet , then the method returns the result false. The following logic helps out to find and print the duplicate values

Now the above hashset contains the unique values.

2. How to find and print duplicate words in a Array / ArrayList / Line using Map (HashMap) 


              Like Set , Map also does not allow duplicates . So when we add a value from the array to HashMap as key using put method , if the key does not exist in the HashMap , then the key is added to HashMap and the put method returns the result null . If the the key is already exist in the HashMap , then the method returns the result as value of the key which is already exist in the map (ie. non null value ). The following logic helps out to print the duplicate words in a line of text . The following sample code splits the words in a line and stores in a arraylist and finds duplicates in the arraylist using HashMap. HashMap accepts both key and value. The values of arraylist is added as Key. Instead of value we need to pass a dummy object. The same thing is happened when we use HashSet bacause HashSet is backed by a HashMap instance. But when you use HashMap , you can use the value field to capture the number of occurrence of each value in the ArrayList . The is the advantage when you use HashMap.

The above hashmap does not contain any duplicates.

The following sample code removes duplicates and also catures the number of occurrence of the duplicate values in the arraylist.

 

3. How to find and remove duplicates in a Array / ArrayList using ArrayList

               In the above section , we have seen how to find and reomve duplicates using HashSet and HashMap . In this section , let us see how to find , print and remove duplicates using second arraylist. When we add en element from the first arraylist to second arraylist , we have to check whether the element is already exist or not in the second arraylist . If it is already exist in the second arraylist , then print it as duplicate, otherwise add the element to the second arraylist. Now the IInd arraylist contanis only unique values. The follwing logic will do .

 

4. Find and Remove duplicates in a Array / ArrayList without using ArrayList, HashSet , HashMap. 
The following code finds and removes the duplicates in the arraylist without using HashSet / ArrayList . The same code can be wriiten using two loops without using lastIndexOf . The below code is not thread safe.

 

The above arrayList1 contains only unique values.

The following complete code helps you to find and print duplicate words or lines in a File. Also helps you to find duplicate words in a Line of text. In the below code , java.util.Scanner class is used to read words or lines from a file and also used to read words from a line. Duplicate words or lines are detected using HashMap. The words or lines are stored as a key in the hashmap. Finally hashmap contains unique words or lines . The value field of hashmap contains the number of occurrence of the words or lines. If the value is greater than 1 , then it is considered as duplicates. To split the lines in a file , new line character (n i.e enter) at the end of line is required . To split the lines using full stop (.) , use useDelimiter(“\.”) .

File k2.txt and output of above program by splitting lines by fullstop (.)

You may also like

Leave a Reply

Be the First to Comment!