Write a program to eliminate the common elements in the given 2 arrays and print only the non-repeating elements and the total number of such non repeating elements.Input Format:The first line contains space separated values, denoting the size of the two arrays in integer format respectively.The next two lines contains the space-separated integer arrays to be compared.Sample Input:5 41 2 8 6 52 6 8 10Sample Output:1 5 103
Question
Write a program to eliminate the common elements in the given 2 arrays and print only the non-repeating elements and the total number of such non repeating elements.Input Format:The first line contains space separated values, denoting the size of the two arrays in integer format respectively.The next two lines contains the space-separated integer arrays to be compared.Sample Input:5 41 2 8 6 52 6 8 10Sample Output:1 5 103
Solution
Here is a Python program that accomplishes the task:
# Function to get the non-repeating elements
def get_non_repeating_elements(arr1, arr2):
# Combine both arrays
combined = arr1 + arr2
# Create an empty list to store non-repeating elements
non_repeating = []
# Iterate over the combined array
for i in combined:
# If an element is not repeating, add it to the list
if combined.count(i) == 1:
non_repeating.append(i)
# Return the non-repeating elements and their count
return non_repeating, len(non_repeating)
# Input sizes of the arrays
n, m = map(int, input().split())
# Input the arrays
arr1 = list(map(int, input().split()))
arr2 = list(map(int, input().split()))
# Get the non-repeating elements and their count
non_repeating, count = get_non_repeating_elements(arr1, arr2)
# Print the non-repeating elements and their count
print(' '.join(map(str, non_repeating)))
print(count)
This program first reads the sizes of the two arrays and the arrays themselves. It then calls a function get_non_repeating_elements that combines the two arrays and iterates over the combined array to find the non-repeating elements. These elements and their count are then printed.
Similar Questions
Write a function, which prints all numbers in common between two given arrays, with each number printed only once. The function returns number of common elements.Assume that both arrays contain each number only once.int common(int *a, int lena, int *b, int lenb);Note: the function does printing.Note: in the output shown, the last number printed is the return value of the function common.
You are given two lists of different lengths of positive integers. Write an algorithm to count the number of elements that are not common to each list.InputThe first line of the input consists of an integer - listInput1_size, an integer representing the number of elements in the first list (N).The second line consists of N space-separated integers representing the first list of positive integers.The third line consists of an integer- listInput2_size, representing the number of elements in the second list (M).The last line consists of M space-separated integers representing the second list of positive integers.OutputPrint a positive integer representing the count of elements that are not common to both the lists of integers.ExampleInput:111 1 2 3 4 5 5 7 6 9 101011 12 13 4 5 6 7 18 19 20Output:12Explanation:The numbers that are not common to both lists are [1, 1, 2, 3, 9, 10, 11, 12, 13, 18, 19, 20].So, the output is 12.
You are given an array of N elements. All elements of the array are in range 1 to N-2. All elements occur once except two numbers, which occur twice. Your task is to find the two repeating numbers.Input FormatThe first line of input contains T - the number of test cases. It's followed by 2T lines, the first line contains N - the size of the array and second line contains the elements of the array.Output FormatPrint the 2 repeated numbers in sorted manner, for each test case, separated by new line.Constraints30 points1 <= T <= 1004 <= N <= 10370 points1 <= T <= 1004 <= N <= 106ExampleInput281 3 2 3 4 6 5 5101 5 2 8 1 4 7 4 3 6Output3 51 4
442. Find All Duplicates in an ArrayMediumTopicsCompaniesGiven an integer array nums of length n where all the integers of nums are in the range [1, n] and each integer appears once or twice, return an array of all the integers that appears twice.You must write an algorithm that runs in O(n) time and uses only constant extra space. Example 1:Input: nums = [4,3,2,7,8,2,3,1]Output: [2,3]Example 2:Input: nums = [1,1,2]Output: [1]Example 3:Input: nums = [1]Output: [] Constraints:n == nums.length1 <= n <= 1051 <= nums[i] <= nEach element in nums appears once or twice.
Given an array of numbers. Give an algorithm for finding the first element in the array which is repeated.Input format :The first line of the input consists of the value of n.The second line of the input consists of the array elements.Output format :The output prints the first repeated element in the array.Refer to the sample input and output for format specifications.Sample test cases :Input 1 :710 5 3 5 3 4 6Output 1 :The first repeating element is 5Input 2 :51 5 6 8 7Output 2 :There are no repeating elementsInput 3 :85 2 4 6 7 2 4 5Output 3 :The first repeating e
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.