Skip to main content

Count words that appear exactly two times in an array of words

Here given code implementation process.

import java.util.HashMap;
/*
    Java program for
    Count words that appear exactly two times in an array of words
*/
public class Occurrence
{
	public void findTwoOccurrence(String[] words, int n)
	{
		int result = 0;
		HashMap < String, Integer > record = 
          new HashMap < String, Integer > ();
		// Count frequency of array elements
		for (int i = 0; i < n; ++i)
		{
			if (record.containsKey(words[i]))
			{	// Increase the frequency
				record.put(words[i], record.get(words[i]) + 1);
			}
			else
			{
              	// Add new element
				record.put(words[i], 1);
			}
		}
		for (String v: record.keySet())
		{
			if (record.get(v) == 2)
			{
              	// When elements are exactly two times
				result++;
			}
		}
		// When result not exist
		System.out.println(" Result : " + result);
	}
	public static void main(String[] args)
	{
		Occurrence task = new Occurrence();
		String[] words = {
			"ip" , "error" , "cool" , "code" , 
              "set" , "game" , "set" , "cool" , 
          	"error" , "cool"
		};
		int n = words.length;
		/*
		    word = ["ip","error","cool" ,
                    "code","set","game" ,
                    "set","cool","error" ,
                    "cool]
		    --------------------------
		    [set,error] appear exactly 2 times
		*/
		task.findTwoOccurrence(words, n);
	}
}

Output

 Result : 2
// Include header file
#include <iostream>
#include <unordered_map>

using namespace std;
/*
    C++ program for
    Count words that appear exactly two times in an array of words
*/
class Occurrence
{
	public: void findTwoOccurrence(string words[], int n)
	{
		int result = 0;
		unordered_map < string, int > record;
		// Count frequency of array elements
		for (int i = 0; i < n; ++i)
		{
			if (record.find(words[i]) != record.end())
			{
				// Increase the frequency
				record[words[i]] = record[words[i]] + 1;
			}
			else
			{
				// Add new element
				record[words[i]] = 1;
			}
		}
		for (auto &v: record)
		{
			if (record[v.first] == 2)
			{
				// When elements are exactly two times
				result++;
			}
		}
		// When result not exist
		cout << " Result : " << result << endl;
	}
};
int main()
{
	Occurrence *task = new Occurrence();
	string words[] = {
		"ip" , "error" , "cool" , "code" , 
      	"set" , "game" , "set" , "cool" , 
      	"error" , "cool"
	};
	int n = sizeof(words) / sizeof(words[0]);
	/*
	    word = ["ip","error","cool",
        "code","set","game"  ,
        "set","cool","error"  , 
        "cool]
	    --------------------------
	    [set,error] appear exactly 2 times
	*/
	task->findTwoOccurrence(words, n);
	return 0;
}

Output

 Result : 2
// Include namespace system
using System;
using System.Collections.Generic;
/*
    Csharp program for
    Count words that appear exactly two times in an array of words
*/
public class Occurrence
{
	public void findTwoOccurrence(String[] words, int n)
	{
		int result = 0;
		Dictionary < string, int > record = 
          new Dictionary < string, int > ();
		// Count frequency of array elements
		for (int i = 0; i < n; ++i)
		{
			if (record.ContainsKey(words[i]))
			{
				// Increase the frequency
				record[words[i]] = record[words[i]] + 1;
			}
			else
			{
				// Add new element
				record.Add(words[i], 1);
			}
		}
		foreach(KeyValuePair < String, int > v in record)
		{
			if (v.Value == 2)
			{
				// When elements are exactly two times
				result++;
			}
		}
		// When result not exist
		Console.WriteLine(" Result : " + result);
	}
	public static void Main(String[] args)
	{
		Occurrence task = new Occurrence();
		String[] words = {
			"ip" , "error" , "cool" , "code" , 
          "set" , "game" , "set" , "cool" , "error" , "cool"
		};
		int n = words.Length;
		/*
		    word = ["ip","error","cool", 
            "code","set","game" ,
            "set","cool","error", "cool]
		    --------------------------
		    [set,error] appear exactly 2 times
		*/
		task.findTwoOccurrence(words, n);
	}
}

Output

 Result : 2
package main
import "fmt"
/*
    Go program for
    Count words that appear exactly two times 
    in an array of words
*/

func findTwoOccurrence(words[] string, n int) {
	var result int = 0
	var record = make(map[string] int)
	// Count frequency of array elements
	for i := 0 ; i < n ; i++ {
		if _, found := record[words[i]] ; found {
			// Increase the frequency
			record[words[i]] = record[words[i]] + 1
		} else {
			// Add new element
			record[words[i]] = 1
		}
	}
	for _, v := range record {
		if v == 2 {
			// When elements are exactly two times
			result++
		}
	}
	// When result not exist
	fmt.Println(" Result : ", result)
}
func main() {

	var words = [] string {"ip","error","cool","code","set",
			"game","set","cool","error","cool"}
	var n int = len(words)
	/*
	    word = ["ip","error","cool" ,
	         "code","set","game" ,
	         "set","cool","error" ,
	         "cool]
	    --------------------------
	    [set,error] appear exactly 2 times
	*/
	findTwoOccurrence(words, n)
}

Output

 Result : 2
<?php
/*
    Php program for
    Count words that appear exactly two times in an array of words
*/
class Occurrence
{
	public	function findTwoOccurrence($words, $n)
	{
		$result = 0;
		$record = array();
		// Count frequency of array elements
		for ($i = 0; $i < $n; ++$i)
		{
			if (array_key_exists($words[$i], $record))
			{
				// Increase the frequency
				$record[$words[$i]] = $record[$words[$i]] + 1;
			}
			else
			{
				// Add new element
				$record[$words[$i]] = 1;
			}
		}
		foreach($record as $key => $value)
		{
			if ($value == 2)
			{
				// When elements are exactly two times
				$result++;
			}
		}
		// When result not exist
		echo(" Result : ".$result.
			"\n");
	}
}

function main()
{
	$task = new Occurrence();
	$words = array("ip", "error", "cool", "code", "set", 
                   "game", "set", "cool", "error", "cool");
	$n = count($words);
	/*
	    word = ["ip","error","cool" ,
	         "code","set","game" ,
	         "set","cool","error" ,
	         "cool]
	    --------------------------
	    [set,error] appear exactly 2 times
	*/
	$task->findTwoOccurrence($words, $n);
}
main();

Output

 Result : 2
/*
    Node JS program for
    Count words that appear exactly two times in an array of words
*/
class Occurrence
{
	findTwoOccurrence(words, n)
	{
		var result = 0;
		var record = new Map();
		// Count frequency of array elements
		for (var i = 0; i < n; ++i)
		{
			if (record.has(words[i]))
			{
				// Increase the frequency
				record.set(words[i], record.get(words[i]) + 1);
			}
			else
			{
				// Add new element
				record.set(words[i], 1);
			}
		}
		for (let [key, value] of record)
		{
			if (value == 2)
			{
				// When elements are exactly two times
				result++;
			}
		}
		// When result not exist
		console.log(" Result : " + result);
	}
}

function main()
{
	var task = new Occurrence();
	var words = ["ip", "error", "cool", "code", 
                 "set", "game", "set", "cool", "error", "cool"];
	var n = words.length;
	/*
	    word = ["ip","error","cool" ,
	         "code","set","game" ,
	         "set","cool","error" ,
	         "cool]
	    --------------------------
	    [set,error] appear exactly 2 times
	*/
	task.findTwoOccurrence(words, n);
}
main();

Output

 Result : 2
#    Python 3 program for
#    Count words that appear exactly two times in an array of words
class Occurrence :
	def findTwoOccurrence(self, words, n) :
		result = 0
		record = dict()
		i = 0
		#  Count frequency of list elements
		while (i < n) :
			if ((words[i] in record.keys())) :
				#  Increase the frequency
				record[words[i]] = record.get(words[i]) + 1
			else :
				#  Add new element
				record[words[i]] = 1
			
			i += 1
		
		for key, value in record.items() :
			if (value == 2) :
				#  When elements are exactly two times
				result += 1
			
		
		#  When result not exist
		print(" Result : ", result)
	

def main() :
	task = Occurrence()
	words = ["ip", "error", "cool", "code", 
             "set", "game", "set", "cool", "error", "cool"]
	n = len(words)
	#    word = ["ip","error","cool" ,
	#         "code","set","game" ,
	#         "set","cool","error" ,
	#         "cool]
	#    --------------------------
	#    [set,error] appear exactly 2 times
	task.findTwoOccurrence(words, n)

if __name__ == "__main__": main()

Output

 Result :  2
#    Ruby program for
#    Count words that appear exactly two times in an array of words
class Occurrence 
	def findTwoOccurrence(words, n) 
		result = 0
		record = Hash.new()
		i = 0
		#  Count frequency of array elements
		while (i < n) 
			if (record.key?(words[i])) 
				#  Increase the frequency
				record[words[i]] = record[words[i]] + 1
			else
 
				#  Add new element
				record[words[i]] = 1
			end

			i += 1
		end

		record.each { | key, value |
			if (value == 2) 
				#  When elements are exactly two times
				result += 1
			end

		}
		#  When result not exist
		print(" Result : ", result, "\n")
	end

end

def main() 
	task = Occurrence.new()
	words = ["ip", "error", "cool", "code", 
             "set", "game", "set", "cool", "error", "cool"]
	n = words.length
	#    word = ["ip","error","cool" ,
	#         "code","set","game" ,
	#         "set","cool","error" ,
	#         "cool]
	#    --------------------------
	#    [set,error] appear exactly 2 times
	task.findTwoOccurrence(words, n)
end

main()

Output

 Result : 2
import scala.collection.mutable._;
/*
    Scala program for
    Count words that appear exactly two times in an array of words
*/
class Occurrence()
{
	def findTwoOccurrence(words: Array[String], n: Int): Unit = {
		var result: Int = 0;
		var record: HashMap[String, Int] = 
          new HashMap[String, Int]();
		var i: Int = 0;
		// Count frequency of array elements
		while (i < n)
		{
			if (record.contains(words(i)))
			{
				// Increase the frequency
				record.addOne(words(i), record.get(words(i)).get + 1);
			}
			else
			{
				// Add new element
				record.addOne(words(i), 1);
			}
			i += 1;
		}
		for ((key, value) <- record)
		{
			if (value == 2)
			{
				// When elements are exactly two times
				result += 1;
			}
		}
		// When result not exist
		println(" Result : " + result);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Occurrence = new Occurrence();
		var words: Array[String] = Array(
          "ip", "error", "cool", "code", "set", 
          "game", "set", "cool", "error", "cool");
		var n: Int = words.length;
		/*
		    word = ["ip","error","cool" ,
		         "code","set","game" ,
		         "set","cool","error" ,
		         "cool]
		    --------------------------
		    [set,error] appear exactly 2 times
		*/
		task.findTwoOccurrence(words, n);
	}
}

Output

 Result : 2
import Foundation;
/*
    Swift 4 program for
    Count words that appear exactly two times in an array of words
*/
class Occurrence
{
	func findTwoOccurrence(_ words: [String], _ n: Int)
	{
		var result: Int = 0;
		var record = [String : Int]();
		var i: Int = 0;
		// Count frequency of array elements
		while (i < n)
		{
			if (record.keys.contains(words[i]))
			{
				// Increase the frequency
				record[words[i]] = record[words[i]]! + 1;
			}
			else
			{
				// Add new element
				record[words[i]] = 1;
			}
			i += 1;
		}
		for (_, value) in record
		{
			if (value == 2)
			{
				// When elements are exactly two times
				result += 1;
			}
		}
		// When result not exist
		print(" Result : ", result);
	}
}
func main()
{
	let task: Occurrence = Occurrence();
	let words: [String] = ["ip", "error", "cool", 
                           "code", "set", "game", 
                           "set", "cool", "error", "cool"];
	let n: Int = words.count;
	/*
	    word = ["ip","error","cool" ,
	         "code","set","game" ,
	         "set","cool","error" ,
	         "cool]
	    --------------------------
	    [set,error] appear exactly 2 times
	*/
	task.findTwoOccurrence(words, n);
}
main();

Output

 Result :  2
/*
    Kotlin program for
    Count words that appear exactly two times in an array of words
*/
class Occurrence
{
	fun findTwoOccurrence(words: Array < String > , n: Int): Unit
	{
		var result: Int = 0;
		val record: HashMap < String, Int > = 
          HashMap < String, Int > ();
		var i: Int = 0;
		// Count frequency of array elements
		while (i < n)
		{
			if (record.containsKey(words[i]))
			{
				// Increase the frequency
				record.put(words[i], record.getValue(words[i]) + 1);
			}
			else
			{
				// Add new element
				record.put(words[i], 1);
			}
			i += 1;
		}
		for ((_, value) in record)
		{
			if (value == 2)
			{
				// When elements are exactly two times
				result += 1;
			}
		}
		// When result not exist
		println(" Result : " + result);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Occurrence = Occurrence();
	val words: Array < String > = arrayOf(
      "ip", "error", "cool", "code", "set", 
      "game", "set", "cool", "error", "cool");
	val n: Int = words.count();
	/*
	    word = ["ip","error","cool"  ,
        	"code","set","game"  ,
        	"set","cool","error"  , 
        	"cool]
	    --------------------------
	    [set,error] appear exactly 2 times
	*/
	task.findTwoOccurrence(words, n);
}

Output

 Result : 2




Comment

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