Count elements present in first array but not in second
Here given code implementation process.
/*
Java Program
Count elements present in first array but not in second
*/
import java.util.HashMap;
public class Counting
{
public void countUnique(int[] first, int n, int[] second, int m)
{
// Use to count frequency
HashMap < Integer, Integer > record = new HashMap < Integer, Integer > ();
// maximum result
int ans = n;
// iterate the loop through by first array size
for (int i = 0; i < n; ++i)
{
if (record.containsKey(first[i]))
{
record.put(first[i], record.get(first[i]) + 1);
}
else
{
record.put(first[i], 1);
}
}
// iterate the loop through by second array size
for (int j = 0; j < m; ++j)
{
if (record.containsKey(second[j]))
{
if (record.get(second[j]) == 1)
{
// remove element
record.remove(second[j]);
}
else
{
// Reduce frequency
record.put(second[j], record.get(second[j]) - 1);
}
ans--;
}
}
// Display calculated result
System.out.print(ans);
}
public static void main(String[] args)
{
Counting task = new Counting();
int[] first = {
9, 7, 4, 8, 7, 3, 9, 9, 8
};
int[] second = {
8, 1, 7, 9 , 9, 5
};
// Get the size of array elements
int n = first.length;
int m = second.length;
task.countUnique(first, n, second, m);
}
}
Output
5
// Include header file
#include <iostream>
#include <unordered_map>
using namespace std;
/*
C++ Program
Count elements present in first array but not in second
*/
class Counting
{
public: void countUnique(int first[], int n, int second[], int m)
{
// Use to count frequency
unordered_map < int, int > record ;
// maximum result
int ans = n;
// iterate the loop through by first array size
for (int i = 0; i < n; ++i)
{
if (record.find(first[i]) != record.end())
{
record[first[i]] = record[first[i]] + 1;
}
else
{
record[first[i]] = 1;
}
}
// iterate the loop through by second array size
for (int j = 0; j < m; ++j)
{
if (record.find(second[j]) != record.end())
{
if (record[second[j]] == 1)
{
// remove element
record.erase(second[j]);
}
else
{
// Reduce frequency
record[second[j]] = record[second[j]] - 1;
}
ans--;
}
}
// Display calculated result
cout << ans;
}
};
int main()
{
Counting task = Counting();
int first[] = {
9 , 7 , 4 , 8 , 7 , 3 , 9 , 9 , 8
};
int second[] = {
8 , 1 , 7 , 9 , 9 , 5
};
// Get the size of array elements
int n = sizeof(first) / sizeof(first[0]);
int m = sizeof(second) / sizeof(second[0]);
task.countUnique(first, n, second, m);
return 0;
}
Output
5
// Include namespace system
using System;
using System.Collections.Generic;
/*
C# Program
Count elements present in first array but not in second
*/
public class Counting
{
public void countUnique(int[] first, int n, int[] second, int m)
{
// Use to count frequency
Dictionary < int, int > record = new Dictionary < int, int > ();
// maximum result
int ans = n;
// iterate the loop through by first array size
for (int i = 0; i < n; ++i)
{
if (record.ContainsKey(first[i]))
{
record[first[i]] = record[first[i]] + 1;
}
else
{
record.Add(first[i], 1);
}
}
// iterate the loop through by second array size
for (int j = 0; j < m; ++j)
{
if (record.ContainsKey(second[j]))
{
if (record[second[j]] == 1)
{
// remove element
record.Remove(second[j]);
}
else
{
// Reduce frequency
record[second[j]] = record[second[j]] - 1;
}
ans--;
}
}
// Display calculated result
Console.Write(ans);
}
public static void Main(String[] args)
{
Counting task = new Counting();
int[] first = {
9 , 7 , 4 , 8 , 7 , 3 , 9 , 9 , 8
};
int[] second = {
8 , 1 , 7 , 9 , 9 , 5
};
// Get the size of array elements
int n = first.Length;
int m = second.Length;
task.countUnique(first, n, second, m);
}
}
Output
5
<?php
/*
Php Program
Count elements present in first array but not in second
*/
class Counting
{
public function countUnique( & $first, $n, & $second, $m)
{
// Use to count frequency
$record = array();
// maximum result
$ans = $n;
// iterate the loop through by first array size
for ($i = 0; $i < $n; ++$i)
{
if (array_key_exists($first[$i], $record))
{
$record[$first[$i]] = $record[$first[$i]] + 1;
}
else
{
$record[$first[$i]] = 1;
}
}
// iterate the loop through by second array size
for ($j = 0; $j < $m; ++$j)
{
if (array_key_exists($second[$j], $record))
{
if ($record[$second[$j]] == 1)
{
// remove element
unset($record[$second[$j]]);
}
else
{ // Reduce frequency
$record[$second[$j]] = $record[$second[$j]] - 1;
}
$ans--;
}
}
// Display calculated result
echo $ans;
}
}
function main()
{
$task = new Counting();
$first = array(9, 7, 4, 8, 7, 3, 9, 9, 8);
$second = array(8, 1, 7, 9, 9, 5);
// Get the size of array elements
$n = count($first);
$m = count($second);
$task->countUnique($first, $n, $second, $m);
}
main();
Output
5
/*
Node Js Program
Count elements present in first array but not in second
*/
class Counting
{
countUnique(first, n, second, m)
{
// Use to count frequency
var record = new Map();
// maximum result
var ans = n;
// iterate the loop through by first array size
for (var i = 0; i < n; ++i)
{
if (record.has(first[i]))
{
record.set(first[i], record.get(first[i]) + 1);
}
else
{
record.set(first[i], 1);
}
}
// iterate the loop through by second array size
for (var j = 0; j < m; ++j)
{
if (record.has(second[j]))
{
if (record.get(second[j]) == 1)
{
// remove element
record.delete(second[j]);
}
else
{
// Reduce frequency
record.set(second[j], record.get(second[j]) - 1);
}
ans--;
}
}
// Display calculated result
console.log(ans);
}
}
function main()
{
var task = new Counting();
var first = [9, 7, 4, 8, 7, 3, 9, 9, 8];
var second = [8, 1, 7, 9, 9, 5];
// Get the size of array elements
var n = first.length;
var m = second.length;
task.countUnique(first, n, second, m);
}
main();
Output
5
# Python 3 Program
# Count elements present in first array but not in second
class Counting :
def countUnique(self, first, n, second, m) :
# Use to count frequency
record = dict()
# maximum result
ans = n
# iterate the loop through by first list size
i = 0
while (i < n) :
if (first[i] in record.keys()) :
record[first[i]] = record.get(first[i]) + 1
else :
record[first[i]] = 1
i += 1
# iterate the loop through by second list size
j = 0
while (j < m) :
if (second[j] in record.keys()) :
if (record.get(second[j]) == 1) :
# remove element
del record[second[j]]
else :
# Reduce frequency
record[second[j]] = record.get(second[j]) - 1
ans -= 1
j += 1
# Display calculated result
print(ans, end = "")
def main() :
task = Counting()
first = [9, 7, 4, 8, 7, 3, 9, 9, 8]
second = [8, 1, 7, 9, 9, 5]
# Get the size of list elements
n = len(first)
m = len(second)
task.countUnique(first, n, second, m)
if __name__ == "__main__": main()
Output
5
# Ruby Program
# Count elements present in first array but not in second
class Counting
def countUnique(first, n, second, m)
# Use to count frequency
record = Hash.new
# maximum result
ans = n
# iterate the loop through by first array size
i = 0
while (i < n)
if (record.key?(first[i]))
record[first[i]] = record[first[i]] + 1
else
record[first[i]] = 1
end
i += 1
end
# iterate the loop through by second array size
j = 0
while (j < m)
if (record.key?(second[j]))
if (record[second[j]] == 1)
record.delete(second[j])
else
record[second[j]] = record[second[j]] - 1
end
ans -= 1
end
j += 1
end
# Display calculated result
print(ans)
end
end
def main()
task = Counting.new()
first = [9, 7, 4, 8, 7, 3, 9, 9, 8]
second = [8, 1, 7, 9, 9, 5]
# Get the size of array elements
n = first.length
m = second.length
task.countUnique(first, n, second, m)
end
main()
Output
5
import scala.collection.mutable._;
/*
Scala Program
Count elements present in first array but not in second
*/
class Counting
{
def countUnique(first: Array[Int], n: Int, second: Array[Int], m: Int): Unit = {
// Use to count frequency
var record = Map[Int, Int]();
// maximum result
var ans: Int = n;
// iterate the loop through by first array size
var i: Int = 0;
while (i < n)
{
if (record.contains(first(i)))
{
record.addOne(first(i), record.get(first(i)).get + 1);
}
else
{
record.addOne(first(i), 1);
}
i += 1;
}
// iterate the loop through by second array size
var j: Int = 0;
while (j < m)
{
if (record.contains(second(j)))
{
if (record.get(second(j)).get == 1)
{
// remove element
record.remove(second(j));
}
else
{
// Reduce frequency
record.addOne(second(j), record.get(second(j)).get - 1);
}
ans -= 1;
}
j += 1;
}
// Display calculated result
print(ans);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Counting = new Counting();
var first: Array[Int] = Array(9, 7, 4, 8, 7, 3, 9, 9, 8);
var second: Array[Int] = Array(8, 1, 7, 9, 9, 5);
// Get the size of array elements
var n: Int = first.length;
var m: Int = second.length;
task.countUnique(first, n, second, m);
}
}
Output
5
import Foundation
/*
Swift 4 Program
Count elements present in first array but not in second
*/
class Counting
{
func countUnique(_ first: [Int], _ n: Int, _ second: [Int], _ m: Int)
{
// Use to count frequency
var record = [Int: Int]();
// maximum result
var ans: Int = n;
// iterate the loop through by first array size
var i: Int = 0;
while (i < n)
{
if (record.keys.contains(first[i]))
{
record[first[i]] = record[first[i]]! + 1;
}
else
{
record[first[i]] = 1;
}
i += 1;
}
// iterate the loop through by second array size
var j: Int = 0;
while (j < m)
{
if (record.keys.contains(second[j]))
{
if (record[second[j]] == 1)
{
// remove element
record.removeValue(forKey: second[j]);
}
else
{
// Reduce frequency
record[second[j]] = record[second[j]]! - 1;
}
ans -= 1;
}
j += 1;
}
// Display calculated result
print(ans, terminator: "");
}
}
func main()
{
let task: Counting = Counting();
let first: [Int] = [9, 7, 4, 8, 7, 3, 9, 9, 8];
let second: [Int] = [8, 1, 7, 9, 9, 5];
// Get the size of array elements
let n: Int = first.count;
let m: Int = second.count;
task.countUnique(first, n, second, m);
}
main();
Output
5
/*
Kotlin Program
Count elements present in first array but not in second
*/
class Counting
{
fun countUnique(first: Array < Int > ,
n: Int, second: Array < Int > , m: Int): Unit
{
// Use to count frequency
var record = mutableMapOf < Int , Int > ();
// maximum result
var ans: Int = n;
// iterate the loop through by first array size
var i: Int = 0;
while (i < n)
{
if (record.containsKey(first[i]))
{
record.put(first[i], record.getValue(first[i]) + 1);
}
else
{
record.put(first[i], 1);
}
i += 1;
}
// iterate the loop through by second array size
var j: Int = 0;
while (j < m)
{
if (record.containsKey(second[j]))
{
if (record.getValue(second[j]) == 1)
{
// remove element
record.remove(second[j]);
}
else
{
// Reduce frequency
record.put(second[j], record.getValue(second[j]) - 1);
}
ans -= 1;
}
j += 1;
}
// Display calculated result
print(ans);
}
}
fun main(args: Array < String > ): Unit
{
var task: Counting = Counting();
var first: Array < Int > = arrayOf(9, 7, 4, 8, 7, 3, 9, 9, 8);
var second: Array < Int > = arrayOf(8, 1, 7, 9, 9, 5);
// Get the size of array elements
var n: Int = first.count();
var m: Int = second.count();
task.countUnique(first, n, second, m);
}
Output
5
In below image is based on ruby code.

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment