You have to write two programs to transform input array into output array. Part 1 Input Array : 2,4,8,5,12,15,6,10,7,30,25,43,46,45,21 Output Array : 2,4,8, 12, 6, 7, 43,46,21, 5,15,10,30,25,45 Part 2 (please note part 2 is different than part 1) Input Array : 2,4,8,5,12,15,6,10,7,30,25,43,46,45,21 Output Array : 2,4,8,21,12,46,6,43 ,7,30,25,10,15,45,5
Answers
Explanation:
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map.Entry;
import java.util.Set;
import java.util.stream.Collectors;
public class DuplicatesInArray
{
//Method 1 : Brute Force Method
private static void findDuplicatesUsingBruteForce(int[] inputArray)
{
for (int i = 0; i < inputArray.length; i++)
{
for (int j = i+1; j < inputArray.length; j++)
{
if(inputArray[i] == inputArray[j])
{
System.out.println("Duplicate Element : "+inputArray[i]);
}
}
}
}
//Method 2 : Sorting Method
private static void findDuplicatesUsingSorting(int[] inputArray)
{
Arrays.sort(inputArray);
for (int i = 0; i < inputArray.length-1; i++)
{
if(inputArray[i] == inputArray[i+1])
{
System.out.println("Duplicate Element : " + inputArray[i]);
}
}
}
//Method 3 : Using HashSet
private static void findDuplicatesUsingHashSet(int[] inputArray)
{
HashSet<Integer> set = new HashSet<Integer>();
for (int element : inputArray)
{
if( ! set.add(element))
{
System.out.println("Duplicate Element : "+element);
}
}
}
//Method 4 : Using HashMap
private static void findDuplicatesUsingHashMap(int[] inputArray)
{
HashMap<Integer, Integer> map = new HashMap<>();
for (int element : inputArray)
{
if(map.get(element) == null)
{
map.put(element, 1);
}
else
{
map.put(element, map.get(element)+1);
}
}
Set<Entry<Integer, Integer>> entrySet = map.entrySet();
for (Entry<Integer, Integer> entry : entrySet)
{
if(entry.getValue() > 1)
{
System.out.println("Duplicate Element : "+entry.getKey()+" - found "+entry.getValue()+" times.");
}
}
}
//Method 5 : Using Java 8 Streams
private static void findDuplicatesUsingJava8(int[] inputArray)
{
Set<Integer> uniqueElements = new HashSet<>();
Set<Integer> duplicateElements = Arrays.stream(inputArray)
.filter(i -> !uniqueElements.add(i))
.boxed()
.collect(Collectors.toSet());
System.out.println(duplicateElements);
}
public static void main(String[] args)
{
int[] inputArray = new int[] {111, 333, 555, 777, 333, 444, 555};
System.out.println("======Duplicates Using Brute Force======");
findDuplicatesUsingBruteForce(inputArray);
System.out.println("======Duplicates Using Sorting======");
findDuplicatesUsingSorting(inputArray);
System.out.println("======Duplicates Using HashSet======");
findDuplicatesUsingHashSet(inputArray);
System.out.println("======Duplicates Using HashMap======");
findDuplicatesUsingHashMap(inputArray);
System.out.println("======Duplicates Using Java 8 Streams======");
findDuplicatesUsingJava8(inputArray);
}
Here are the two programs:
1st program
// Program to take 5 values from the user and store them in an array
// Print the elements stored in the array
#include <stdio.h>
int main() {
int values[5];
printf("Enter 5 integers: ");
// taking input and storing it in an array
for(int i = 0; i < 5; ++i) {
scanf("%d", &values[i]);
}
printf("Displaying integers: ");
// printing elements of an array
for(int i = 0; i < 5; ++i) {
printf("%d\n", values[i]);
}
return 0;
}
Output:
Enter 5 integers: 1
-3
34
0
3
Displaying integers: 1
-3
34
0
3
2nd program:
# Python program to demonstrate
# Creation of Array
# importing "array" for array creations
import array as arr
# creating an array with integer type
a = arr.array('i', [1, 2, 3])
# printing original array
print ("The new created array is : ", end =" ")
for i in range (0, 3):
print (a[i], end =" ")
print()
# creating an array with double type
b = arr.array('d', [2.5, 3.2, 3.3])
# printing original array
print ("The new created array is : ", end =" ")
for i in range (0, 3):
print (b[i], end =" ")
Output:
Enter 5 integers: 1
-3
34
0
3
Displaying integers: 1
-3
34
0
3
#SPJ2