OneCompiler

Scala program

Write a Scala program to create two sets of strings and find common strings between them. Merge sets after removing common strings. Display resultant set.

2 Answers

3 years ago by

let arr1 = ["welcome", "community", "information"];
let arr2 = ["community", "social", "social"];

let commonStrings = arr1.filter(value => arr2.includes(value));
let mergedArr = [...new Set([...arr1, ...arr2])];

console.log("Common Strings: ", commonStrings);
console.log("Merged Array: ", mergedArr);

3 years ago by Ravi Panthula

import scala.collection.mutable

// Create two sets of strings
val set1 = Set("apple", "banana", "orange")
val set2 = Set("banana", "kiwi", "mango")

// Find common strings
val common = set1.intersect(set2)

// Remove common strings from each set
val set1WithoutCommon = set1 -- common
val set2WithoutCommon = set2 -- common

// Merge sets without common strings
val merged = set1WithoutCommon ++ set2WithoutCommon

// Display resultant set
println(merged)

3 years ago by Fir