like

function like( arr, other ) --> res

Description

Check if both array tables are likewise. Tables are likewise than they have the identical number of the same values. The positons of the values can be different.

Parameters

arr

An array table you want to compare.

other

An array table you want to compare.

Return Values

res

true if the both array tables are likely, and false then they are to different.

Code

--ZFUNC-like-v1
local function like( arr, other ) --> res
   local tmp = {}

   for i, v in ipairs( arr ) do
      local base = tmp[ v ] or 0
      tmp[ v ] = base + 1
   end

   for i, v in ipairs( other ) do
      local base = tmp[ v ] or 0
      tmp[ v ] = base + 1
   end

   for k, v in pairs( tmp ) do
      if v % 2 ~= 0 then
         return false
      end
   end

   return true
end

return like

Examples

local t = require( "tapered" )
local like = require( "like" )

t.ok( like( { "a", "b", "c" }, { "b", "c", "a" } ) )
t.ok( like( { "a", "c", "c" }, { "c", "c", "a" } ) )
t.nok( like( { "a", "b", "c" }, { "a", "b", "d" } ) )

t.done()

See also